Reputation: 337
I'm wondering which is the better way of these two ones to get the file extension, and check if it belongs to a list.
name, ext = os.path.splitext(filename)
return ext == ".pdf" # an example
or
return filename.endswith(".pdf")
Upvotes: 5
Views: 5529
Reputation: 5384
Here are two examples of checking with both methods if a filename contains any of the extensions.
ext = ('.txt', '.py', '.docx', '.pdf') # tuple of extensions.
filenames = [ ... ] # list of filename strings
ends_matches = [ f for f in filenames if f.endswith(ext) ]
# change ext to set for slightly better efficiency.
split_matches = [ f for f in filenames if f.splitext()[1] in ext ]
# may need to include .lower() for cases with capital extensions.
In this case it is really up to you which you want to use. If you just want to check a single file extension then I would suggest with the latter, endswith
.
return filename.endswith(extension)
Upvotes: 6
Reputation: 3525
endswith()
is commonly used to check for file names. A pythonic approach to filter out certain file extensions in a list of file names could be done as follows:
>>> extensions = ('.pdf', '.txt', '.rtf')
>>> [file_name for file_name in files if file_name.lower().endswith(extensions)]
# Output -> File names that end with any of those extensions.
With this sort of approach you can provide many file extensions in a tuple, and potentially eliminating an if/else
ladder (clean code :D).
Upvotes: 1
Reputation: 34911
Second one is cleaner and shorter solution I would say if you need to only check for specific extension. Both work even on special cases like filename = abc.def.pdf
.
First one is better if you need to process somehow filename and/or extension later.
Upvotes: 1