这是python脚本是我到目前为止所提出的。我只想要一个目录列表以及大小超过500mb的文件,内置的os.walk()
也返回一个子目录列表。所以,我通过引用这个post来调整它。
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'
错误是什么意思?有没有更简单的做事方式?
答案 0 :(得分:2)
函数walk_free(path)
生成(path, filenames)
的元组。文件名只是文件名,不包括该文件的完整路径。
尝试替换此
os.path.getsize(f)
用这个:
os.path.getsize(os.path.join(direc, f))