User
User

Reputation: 826

os.listdir() gives a list of files not correct

I'm getting this weird problem. I must take all the list of files inside a folder but the result of os.listdir() gives me a list of a length different from the exact number of files inside that folder. I've also printed this list to check if it takes also something else but not.

TRAINING_FILES = os.listdir(TRAINING_DATA)  # length = 7269
with open(LOG_DIR + 'output.txt', 'w') as f:
    for p in TRAINING_FILES: f.write(p + '\n')

In this log file I have only .bin files, so it takes stuff correctly.

Inside the folder, I've taken all the files with cmd + a and dragged outside to display the number which is 7268 and I also checked with the terminal using ls | wc -l and I have 7268 files.

Is there something that I don't know?

Upvotes: 0

Views: 1370

Answers (1)

Sharku
Sharku

Reputation: 1092

I have a answer to propose to you: Could it be that ls | wc -l gives you all the files except . .. and .DS_Store. But python os.listdir(path) gives you all the files except . and ... So your missing file is only .DS_Store

os.listdir(path)

Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.

Upvotes: 4

Related Questions