如何获取windows上文件大小小于500mb的文件列表?

2024-09-30 16:29:19 发布

您现在位置:Python中文网/ 问答频道 /正文

这是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'

这个错误是什么意思?有没有更简单的做事方法?你知道吗


Tags: pathnameinpyforifostop