If os.path.I'm a beginner and i just started with automate the boring stuff with python course and reached his first os lesson, so i decided to make a recursive directory size finder since apparently os.path.getsize doesn't get the size of folders. Oldbakup = os.path.join(newdir, file + '.bak.' + str(index-1)) # no need to backup if file and last version are identical # create directory named 'bak' in current directoryīakup = os.path.join(newdir, file + '.bak.' + str(index)) # check for existence of previous versions "Back up files or files with extension in passed tuple types"Ībspath = os.path.abspath(os.path.join(dir, file))Įxt=lower((os.path.splitext(file))) The new backup(), modified in this way, becomes: def backup(dir, types=None, files=None): Removed variable "bakuppath" (i was getting an error) and using "bakup" for copy() Made types an optional parameter, with default value None meaning all typesĪdded files parameter to initail (non-recursive) backup call, I have made slight adjustments to this function: isdir ( tree_top ): backup_files ( tree_top, bakfolder )Ībility to specify files by name. argv ))) else : bakfolder = BAKFOLDER if os. copy ( filepath, backup ) except ( OSError, IOError ), e : pass if _name_ = "_main_" : if len ( sys. exists ( backup ): print 'Copying %s to %s. cmp ( abspath, filepath, shallow = False ): continue except OSError : pass try : if not os. exists ( old_backup ): break abspath = os. %2.2d ' % ( destpath, index - 1 ) if not os. abspath ( filepath ) if index > 0 : # No need to backup if file and last version # are identical old_backup = ' %s. %2.2d ' % ( destpath, index ) abspath = os. join ( backup_dir, f ) # Check existence of previous versions for index in xrange ( MAXVERSIONS ): backup = ' %s. makedirs ( backup_dir ) # To avoid recursing into sub-directories subdirs = for f in files : filepath = os. join ( bakdir_name, top_dir, relpath ) else : backup_dir = os. replace ( tree_top, '' ) backup_dir = os. Instead backups of files in ~/programs will be inside ~/backups/programs, backups of files in ~/programs/python will be inside ~/backups/programs/python etc. The command $ python backup.py ~/programs ~/backups will create backups of every file inside ~/backups/programs folder. For example, the backups of files inside ~/programs will be found in ~/programs/.bak, the backup of files inside ~/programs/python in ~/programs/python/.bak etc. Maximum of 'MAXVERSIONS' simultaneous versions can be maintained Example usage - The command $ python backup.py ~/programs will create backups of every file inside ~/programs inside sub-directories named '.bak' inside each folder. If another directory path is passed as value of this argument, the backup versions are created inside that directory instead. By default the backup folder is a folder named '.bak' created inside the folder which is backed up. Takes the top-level directory and an optional backup folder name as arguments. # Credit: Anand Pillai, Tiago Henriques, Mario Ruggier import sys, os, shutil, filecmp MAXVERSIONS = 100 BAKFOLDER = '.bak' def backup_files ( tree_top, bakdir_name = BAKFOLDER ): """ Directory back up function. #!/usr/bin/env python # Backup files - As published in Python Cookbook # by O'Reilly with some bug-fixes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |