alvas
alvas

Reputation: 122142

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?

Upvotes: 0

Views: 70

Answers (2)

XORcist
XORcist

Reputation: 4367

How about this:

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

Upvotes: 0

YXD
YXD

Reputation: 32521

One idiomatic way is to do

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

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

Upvotes: 4

Related Questions