os.listdir outputting different files than there are in the folder

时间:2015-05-04 19:42:34

标签: python file-io

here is my code sample:

if os.path.exists(inputdir):
    for file in os.listdir(inputdir):
        print file

So when I print the files, files that do not actually exist in the input directory are printed so when I try to use the file it cannot open it. What could be causing this issue?

EDIT: Out of 8,831 files at the beginning this gets printed:

0004872bda8db3ea40167b887108e9a3.xml
0007cba714860a090922cec6132442a4.xml
0009f949eba51c5d2936458595040135.xml

When the actual files are:

000cbd88e8868d56aa7eb7a7e3e90166.xml
00a270aa4d18f139d0405604df200c61.xml
00a39768b77e83f86bd3cd81bf5a9cfb.xml

And at the end the files are identical. I'm not sure at what point it changes.

1 个答案:

答案 0 :(得分:-1)

这应该有效:

import os
for entry in sorted(os.listdir(inputdir)):
  filename = os.path.join(inputdir, entry)
  print(filename)
  os.lstat(filename)

如果您的系统没有os.lstat,请尝试os.stat

获得No such file or directory的一个可能原因是filename是指向不存在文件的符号链接。