如何获得文件大小小于500mb的文件列表?
问题描述:
这是python脚本,是我到目前为止。我只想要一个目录列表以及大于500MB的文件,内置的os.walk()
也会返回一个子目录列表。所以,我通过参考post来调整。如何获得文件大小小于500mb的文件列表?
import os
import sys,getopt
def do_stuff(path):
def walk_free(top,topdown=True,onerror=None,followlinks=False):
islink,isdir,join=os.path.islink,os.path.isdir,os.path.join
try:
names=os.listdir(top)
except Exception as e:
print e
return
dirs,nondirs=[],[]
for name in names:
if isdir(join(top,name)):
dirs.append(name)
else:
nondirs.append(name)
if topdown:
yield top,nondirs
for name in dirs:
new_path=join(top,name)
if followlinks or not islink(new_path):
for x in walk_free(new_path,topdown,onerror,followlinks):
yield x
if not topdown:
yield top,nondirs
with open("delete.txt",'a+') as output:
output.write(" PATH | FILE \n")
for direc,files in walk_free(path):
del_list=(str(f) for f in files if os.path.getsize(f)//(2**20) > 500)
for file in del_list:
output.write(" %s | %s \n" %(str(direc),file))
if __name__=="__main__" :
do_stuff(str(sys.argv[1]))
当运行它,堆栈跟踪是:
C:\Users\d\Desktop>python cleaner.py C:\
Traceback (most recent call last):
File "cleaner.py", line 35, in <module>
do_stuff(str(sys.argv[1]))
File "cleaner.py", line 32, in do_stuff
for file in del_list:
File "cleaner.py", line 31, in <genexpr>
del_list=(str(f) for f in files if os.path.getsize(f)//(2**20) > 500)
File "C:\Python27\lib\genericpath.py", line 49, in getsize
return os.stat(filename).st_size
WindowsError: [Error 2] The system cannot find the file specified: '1Clickfolder
test.txt'
是什么错误呢?有没有更简单的做事方式?
答
函数walk_free(path)
生成(path, filenames)
的元组。文件名只是文件名,它不包括该文件的完整路径。
尝试更换此
os.path.getsize(f)
与此:
os.path.getsize(os.path.join(direc, f))
+0
日Thnx它完美地现在工作! – akshay 2014-08-29 13:18:21
你可以只用'os.walk(路径)'而忽略子目录列表:) – Messa 2014-08-29 12:34:39