简体   繁体   English

os.listdir()给出了不正确的文件列表

[英]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. 我必须获取文件夹中所有文件的列表,但是os.listdir()的结果为我提供了一个列表,该列表的长度不同于该文件夹中文件的确切数量。 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. 在此日志文件中,我只有.bin文件,因此它可以正确处理内容。

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 在文件夹内,我用cmd + a提取了所有文件,然后拖动到外面以显示数字7268并且还使用ls | wc -l查看了终端ls | wc -l ls | wc -l and I have 7268 files. ls | wc -l ,我有7268文件。

Is there something that I don't know? 有我不知道的东西吗?

I have a answer to propose to you: Could it be that ls | wc -l 我有一个向您建议的答案:可能是ls | wc -l ls | wc -l gives you all the files except . ls | wc -l为您提供除以外的所有文件. .. and .DS_Store . ...DS_Store But python os.listdir(path) gives you all the files except . 但是python os.listdir(path)会为您提供除以外的所有文件. and .. . .. So your missing file is only .DS_Store 因此,您丢失的文件只有.DS_Store

os.listdir(path)

Return a list containing the names of the entries in the directory given by path. 返回一个列表,其中包含由path给出的目录中条目的名称。 The list is in arbitrary order. 该列表是任意顺序的。 It does not include the special entries '.' 它不包括特殊条目“。” and '..' even if they are present in the directory. 和“ ..”,即使它们存在于目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM