0
我创建了下面的Python脚本,列出目前在directoris /子目录清单在UNIX目录/子目录
代码A
files = glob.glob("%s/*.*"%os.getcwd())
sorted_file = sorted(files, key=os.path.getsize)
for path, dirs, files in os.walk(os.getcwd()):
for d in dirs:
for f in glob.iglob(os.path.join(path, d, '*.*')):
print f ,os.path.getsize(f)
所有文件及其大小的所有文件我收到以下错误,当它运行trhough目录:
Traceback (most recent call last):
File "Test.py", line 27, in <module>
print f ,os.path.getsize(f)
File "/usr/lib/python2.6/genericpath.py", line 49, in getsize
return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: '/My/Folder/Perlx.x'
奇怪的是,当我去到UNIX中的/My/Folder/
箱并做了ls -l
,我可以看到Perlx.x
whihc它truns出来是Symbolic Link
代码B
for path, subdirs, files in os.walk(os.getcwd()):
for name in files:
f = os.path.join(path, name)
print f,os.path.getsize(f)
错误:
/My/Folder/BEA
Traceback (most recent call last):
File "Test.py", line 19, in <module>
print f,os.path.getsize(f)
File "/usr/lib/python2.6/genericpath.py", line 49, in getsize
return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: '/My/Folder/BEA'
在这两种情况下,BEA和Perlx.x是Symbolic Links
它退出指定的文件夹。我如何摆脱这个错误?
为什么你认为你需要调用'glob.igob'。 'walk'命令将产生'os.getcwd()'下的所有目录和文件' –
另外,不要使用'glob.glob(“.../*。*”)';相反,在目标目录上使用'os.listdir'会更快更干净。 – nneonneo
我无法重现错误。难道是有一些其他进程移动文件? – Javier