![]() ![]() %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. Src: A string representing the path of the source directory.#!/usr/bin/env python # Backup files - As published in Python Cookbook # by O'Reilly with some bug-fixes. Syntax: pytree(src, dst, symlinks = False, ignore = None, copy_function = copy2, ignore_dangling_symlinks = False) Permissions and times of directories are copied with copystat() and individual files are copied using py2(). The destination directory, named by (dst) must not already exist. pytree() method recursively copies an entire directory tree rooted at source (src) to the destination directory. This module helps in automating process of copying and removal of files and directories. It comes under Python’s standard utility modules. Shutil module in Python provides many functions of high-level operations on files and collections of files. How to get column names in Pandas dataframe.Adding new column to existing DataFrame in Pandas. ![]() How to use Glob() function to find files recursively in Python?.Python: Check if a File or Directory Exists.Rename all file names in your directory using Python.How to move Files and Directories in Python.Python | Move or Copy Files and Directories.Copy a directory recursively using Python (with examples).ISRO CS Syllabus for Scientist/Engineer Exam. ![]()
0 Comments
Leave a Reply. |