I'm trying to get the file count in a given directoy using python, but I'd like to exclude .DS_Store
files.
How can that be done?
count = len([name for name in os.listdir(DIR)])
I'm trying to get the file count in a given directoy using python, but I'd like to exclude .DS_Store
files.
How can that be done?
count = len([name for name in os.listdir(DIR)])
count = len([name for name in os.listdir(DIR) if name != ".DS_Store"])
Or to exclude all hidden files, like jonrsharpe suggested:
count = len([name for name in os.listdir(DIR) if name.startswith(".")])
You can use an if
within your list comprehension :
count = len([name for name in os.listdir(DIR) if name !='.DS_Store'])
Or if you don't want a specific format you can use str.endswith
:
count = len([name for name in os.listdir(DIR) if if not name.endswith('DS_Store')])