2014-08-29 133 views
0

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

是什麼錯誤呢?有沒有更簡單的做事方式?

+0

你可以只用'os.walk(路徑)'而忽略子目錄列表:) – Messa 2014-08-29 12:34:39

回答

2

函數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