Remove unnecessary large files from the system using python

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.