Home Blog Articles Conference Portfolio Flash-point RSS  RU EN

Volodymyr Bondarenko

Web Technology Expert
RECENT POSTS
  •  Writing program for cleaning the crowded C: drive on Python
  •  Encoding information using Reed-Solomon codes
  •  What is the differents between Apache Cordova and PhoneGap?
  •  Updated map of subway and trains of Berlin - 02.09.2013

  • TAG CLOUD
    Ajax CMS conference data coding Flash-point friendly URL mobile development PHP presentation programming Python script SEO technology video
    CONTACT

    Skype: coolweb_ua

    twitter

    COUNTER


    Writing program for cleaning the crowded C: drive on Python

     

    Writing program for cleaning the crowded C: drive in Python

    New updates of Windows occupied 600Mb on hard drive. From 100 Gb of space on drive C: left only 300 Mb empty space. In folder Downloads every big file was already deleted. Cleaning cache with CCleaner could help only for few days. It's time to make global cleaning of system. During two last years my Windows stored plenty of useless data that need to be found and removed. File size has become the main search criteria. I decided manually go through all files that occupy more then 50 Mb. In Linux it took only one command to solve this problem:

    find / -type f | xargs du -sh | sort -n | tail -50

    But in Linux there would not be such problem in first place. For Windows exists plenty of software that could solve this problem. Even Total Commander could do it using additional search. But I decides to take a challenge and create own solution on Python. 

    First version of program was very plain:

    import os
    
    def searchBigFiles(path, bigSize):    
        folder=os.listdir(path)
        if path=='\\':
            path=''
        for element in folder:
            if os.path.isfile(path+'\\'+element):
                statinfo=os.stat(path+'\\'+element)
                if(statinfo.st_size>1024*1024*bigSize):
                    size=round(statinfo.st_size/(1024*1024),1)
                    print(str(size)+' Mb - '+path+'\\'+element)
            elif os.path.isdir(path+'\\'+element):
                searchBigFiles(path+'\\'+element, bigSize)
                    
    searchBigFiles('\\xampp', 5)
    


    Program shows all files that weigh more than 5Mb that situated in directory \xampp. But using it for root of drive C:, I got exception:

    PermissionError: [WinError 5] Отказано в доступе: '\\Documents and Settings\\*.*'


    Even launch file with Administrator access, I got this two types of errors:

    • PermissionError — this error apeared for 89 folders from 85984 checked by script. Some directories like"Documents and Settings" doesn't exist for real, but os.path.isdir shows True.
    • FileNotFoundError — was only ones for program Evernote.


    I added block try-catch, and got code, that went through drive C: without errors:

    import os
    
    def searchBigFiles(path, bigSize):    
        try:
            folder=os.listdir(path)
            if path=='\\':
                path=''
            for element in folder:
                if os.path.isfile(path+'\\'+element):
                    statinfo=os.stat(path+'\\'+element)
                    if(statinfo.st_size>1024*1024*bigSize):
                        size=round(statinfo.st_size/(1024*1024),1)
                        print(str(size)+' Mb - '+path+'\\'+element)
                elif os.path.isdir(path+'\\'+element):
                    searchBigFiles(path+'\\'+element, bigSize)
        except PermissionError:
            print('no access to folder: '+path)       
        except FileNotFoundError:
            print('folder doesn\'t exist: '+path)       
                    
    searchBigFiles('\\', 100)
    



    I spent 30 minutes more, made tuning of script: converted to OOP, added sorting by size, make nice output format. Final version of code:

    import os
    
    class CleanDiskC:
    
        total=0;
        countFiles=0;
        countFolders=0;
        fileNotFoundError=list()
        permissionError=list()
        files=list()
    
        def __init__(self, bigSize=50, state=True):
            self.bigSize=bigSize
            self.stateless=state
    
        def printRow(self, size, path):
            size=str(size)+'Mb'
            while(len(size)<8):
                size+=' '
            print('|| '+size+'|| '+path)
        
        def findBigFiles(self, path):
            if not self.stateless:
                print('Progress: |', end='')
            self.searchBigFiles(path)       
            if not self.stateless:
                if len(self.fileNotFoundError)>0:
                    print('\n\nCan\'t find files:')
                    print('----------------------------')
                    for elem in self.fileNotFoundError:
                        print(elem)
                    print('----------------------------')
                    print('Total: '+str(len(self.fileNotFoundError))+' elements\n')
                if len(self.permissionError)>0:
                    print('\n\nDon\'t have pernission to access folder:')
                    print('----------------------------')
                    for elem in self.permissionError:
                        print(elem)
                    print('----------------------------')
                    print('Total: '+str(len(self.permissionError))+' folders\n')
                print('\nFiles with size more then '+str(self.bigSize)+'Mb:')
                print('----------------------------')
                self.files.sort(key=lambda x: x['size'], reverse=True)
                for elem in self.files:
                    self.printRow(elem['size'], elem['path'])
            print('----------------------------')
            print('Total big files store: '+str(round(self.total/1024,1))+'Gb')
            print('Total cheched: '+str(self.countFiles)+' files')
            print('Total checked: '+str(self.countFolders)+' folders')
                
            
        def searchBigFiles(self, path):
            self.countFolders+=1        
            try:
                folder=os.listdir(path)
                if path=='\\':
                    path=''
                for element in folder:
                    if os.path.isfile(path+'\\'+element):
                        self.countFiles+=1
                        if not self.stateless:
                            if self.countFiles%10000==0:
                                print(str(self.countFiles)+'|',end='')
                        statinfo=os.stat(path+'\\'+element)
                        if(statinfo.st_size>1024*1024*self.bigSize):
                            size=round(statinfo.st_size/(1024*1024),1)
                            self.total+=size
                            if self.stateless:
                                self.printRow(size, path+'\\'+element)
                            else:
                                self.files.append({'size':size, 'path':path+'\\'+element})
                    elif os.path.isdir(path+'\\'+element):           
                            self.searchBigFiles(path+'\\'+element)
            except PermissionError:
                if self.stateless:
                    print('no access to folder: '+path)
                else:
                    self.permissionError.append(path)
            except FileNotFoundError:
                if self.stateless:
                    print('folder doesn\'t exist: '+path)
                else:
                    self.fileNotFoundError.append(path)
    
    #bigSize: default=50Mb
    #stateless: default=True
                
    do=CleanDiskC(50, False)
    do.findBigFiles('\\')


    After script was ready I returned to main task with hard drive. I removed:

    • .Trash-1000 — in root of drive C: was hidden folder, that can be created by Linux OS with big removed files — 2,5 Gb
    • VirtualBox project with snapshots  — 5 Gb
    • backups of iPad, iPhone (in settings of iTunes was another drive) — 7 Gb
    • forgotten previously downloaded files — 2 Gb
    • files of video projects  — 10 Gb


    At least 27 Gb of free space appeared on drive C:. Such Такой approach gave opportunity more closely get know files of operation system Windows and it interaction with Python.


    Tags:  Python, программирование, скрипт, технологии

    Read on:

    loading

    Leave comment

    Name:
    Mail (hide):
    Site:
    Text: :) :( 0_o =-0 =-D 8-) :-(( TT >:o ]:->

    Using of any site materials is possible only when placing an active and direct links VBond.Kiev.ua.

    Home | Blog | Articles | Conference | Portfolio | Flash-point | RSS

    developed by Bondarenko Volodymyr