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