简体   繁体   中英

How to skip initializing the 1st returned value from a function? - Python

Often i only need the 2nd thing that was returned from a function; for example the code below where i want get a list of .txt files and get the filenames without the extension:

import os, glob
indir = "/home/alvas/whatever/"
for infile in glob.glob(os.path.join(indir,'*'):
  PATH, FILENAME = os.path.split(infile)
  FILENAME.rstrip(".txt")

Instead of doing:

PATH, FILENAME = os.path.split(infile)

I could also do:

FILENAME = os.path.split(infile)[1]

Are there other ways of doing this?

One idiomatic way is to do

_, FILENAME = os.path.split(infile)

With the convention that _ is a variable that you are discarding or ignoring.

这个怎么样:

_, filename = os.path.split(infile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM