1

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)])

Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158

2 Answers2

3
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(".")])
Community
  • 1
  • 1
heinst
  • 8,520
  • 7
  • 41
  • 77
1

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')])
Mazdak
  • 105,000
  • 18
  • 159
  • 188