Python removed excessive large file files from the system and suddenly found that the hard disk space was insufficient one day. so I wrote a python script to search for all files larger than 10 MB, check whether there are duplicate copies of these large files. If yes, all are listed for manual deletion.
Add a specified directory parameter
For example, python redundant_remover.py/tmp
The stat module and OS and sys system modules are mainly used.
Import OS, sys # introduce the statistical module from stat import * BIG_FILE_THRESHOLD = 0000000ldict1 = {}# filesize for key, filename for valuedict2 ={}# filename for key, filesize: valuedef treewalk (path): try: for I in OS. listdir (path): mode = OS. stat (path + "/" + I ). st_mode if S_ISDIR (mode) <> True: filename = path + "/" + I filesize = OS. stat (filename ). st_size if filesize> BIG_FILE_THRESHOLD: if filesize in dict1: dict2 [filename] = filesize dict2 [dict1 [filesize] = filesize else: dict1 [filesize] = filename else: treewalk (path + "/" + I) handle T WindowsError: passdef printdict (finaldict): for I _size in finaldict. values (): print I _size for j_name in finaldict. keys (): if finaldict [j_name] = I _size: print j_name print "\ n" if _ name __= = "_ main _": treewalk (sys. argv [1]) printdict (dict2)