简体   繁体   English

Python function os.listdir 不显示目录中的所有文件

[英]Python function os.listdir doesnt show all files in directory

Hi I'm trying load couple of files with os.listdir but it skips several files.嗨,我正在尝试使用os.listdir加载几个文件,但它会跳过几个文件。

Format of files in directiy is: directiy文件格式为:

a_0.csv
a_1.csv
a_2.csv
  ·
  ·
  ·
a_25.csv

But the os.listdir only take file 0,1,10,11.. File with numbers 3,4,5... aren't in in output object.但是os.listdir只取文件0,1,10,11..编号为3,4,5...的文件不在 output object 中。

And here is my function:这是我的 function:

def best_fit_from_attemp(self):
    arr =[]
    for i in os.listdir(self.path_to_files):
        print("i",i)
        if i[0] == 'f' and  i[1] == 'i' and i[2] == 't':
            print("searching for smallest number... ",i)
            arr.append(self.find_min_fitness(i))

Output: Output:

i fitness_gen_0.csv
searching for smallest number...  fitness_gen_0.csv
i fitness_gen_1.csv
searching for smallest number...  fitness_gen_1.csv
i fitness_gen_10.csv
searching for smallest number...  fitness_gen_10.csv
i fitness_gen_11.csv
searching for smallest number...  fitness_gen_11.csv

My guess is that the filenames are all there, just not in the order you expect.我的猜测是文件名都在那里,只是没有按照您期望的顺序。

The file names are strings.文件名是字符串。 They are sorted according to string-sorting logic: imagine replacing every 0 with an a , every 1 with a b , every 2 with a c and so on, and now think about how the strings would be ordered alphabetically.它们根据字符串排序逻辑进行排序:想象将每个 0 替换为a ,将每个 1 替换为b ,将每个 2 替换为c等等,现在考虑如何按字母顺序对字符串进行排序。 Ordinary string sorting doesn't know that we like to write numbers with the least-significant-digit last .普通的字符串排序不知道我们喜欢用最不重要的数字last来写数字。 In ordinary string sorting, it really does go 1 , then 10 , then 11 —that's analogous to ordering the strings b , then ba , then bb , which is the correct dictionary ordering.在普通的字符串排序中,它确实是 go 1 ,然后是10 ,然后是11 ——这类似于对字符串进行排序b ,然后是ba ,然后是bb ,这是正确的字典排序。 It will go on for a long time before it gets to anything starting with 3 (analogously, the words in the dictionary that start with d come much later than anything starting with b ).它将 go 开启很长一段时间,然后才能到达以3开头的任何内容(类似地,字典中以d开头的单词比以b开头的单词要晚得多)。

You could investigate algorithms/packages that do "natural sorting".您可以研究执行“自然排序”的算法/包。 Or, the greatly preferable best practice would be simply to fix your file naming convention to start with: ensure that the numbers embedded in your filenames all have the same number of digits, with leading zeros where necessary ( 001 , 002 , etc).或者,更可取的最佳做法是简单地修复文件命名约定:确保文件名中嵌入的数字都具有相同的位数,必要时带有前导零( 001002等)。

I would be pretty sure that the problem lies in the function self.find_min_fitness(i) .我很确定问题出在 function self.find_min_fitness(i)上。 Some parts in it must cause the program to break after listing the first couple of files.在列出前几个文件后,其中的某些部分必须导致程序中断。

The files are sorted by name by os.listdir() .文件由os.listdir()按名称排序。 It prefers 1 over 2 when listing the files, that's why all the filenames starting with 1 are listed first.列出文件时它更喜欢1而不是2 ,这就是为什么所有以1开头的文件名都首先列出的原因。

As a solution: Maybe try and comment out this one line where self.find_min_fitness(i) is called and see whether the problem still occurs作为解决方案:也许尝试注释掉调用self.find_min_fitness(i)的这一行,看看问题是否仍然存在

This seems questionable.这似乎值得怀疑。

self.find_min_fitness(i)

Well I would suggest you try something like this.好吧,我建议你尝试这样的事情。

Below directory contains a list of files numbered.下面的目录包含编号的文件列表。

files = os.listdir("C:/Users/Danish/Documents/JavaScriptPrimerRepo/MyJSCodes")

for i in files:
    print(i)

This is the sample output below.这是下面的示例 output。

00_BlankPage.html
01_DataType.html
02_LinkingExternalJsfile.html
02_Script.js
03_VariableMutation_cohersion.html
04_Operators.html
05_SampleCodeCalculatingBMI.html

NOTE: I stored the result in separate variable before iterating and importantly fileName starts with number first.注意:我在迭代之前将结果存储在单独的变量中,重要的是 fileName 首先以数字开头。

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

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