1.0 using Os.system to compress will depend on the computer outside of the program, using the ZipFile built-in module to create a compressed document is a good improvement, in addition to use date time to name the compressed file is not very comprehensive, you want to increase the user input file name and enhance the document archiving function.
ZipFile module Zipfile.write (filename[, arcname[, Compress_type])
Adds the specified file to the zip document. FileName is the file path, Arcname is the name saved after adding to the zip document, the parameter Compress_type represents the compression method, and its value can be zipfile. zip_stored or zipfile. zip_deflated.
Import zipfile>>> z=zipfile. ZipFile (R'D:\backup\te.zip','w')>>> Z.write (R'd:qqpcmgr')>>> z.close ()
Compress an entire folder learn Os.walk first.
Iterate over all files in this directory
Overview
The Os.walk () method is used to output the file name in the directory through the directory tree Walk, up or down.
Grammar
The syntax format for the Walk () method is as follows:
Os. Walk(top[, topdown=True[, onerror=None[, followlinks= False]])
Parameters
Top -Every folder in the root directory (including itself), resulting in 3-tuple (Dirpath, dirnames, filenames) "Folder path, folder name, file name".
Topdown --optional, true or unspecified, a 3-tuple of a directory will be generated first (directory top-down) than the 3-tuple of any of its subfolders. If Topdown is False, a 3-tuple of a directory will be generated after the 3-tuple of any of its subfolders (directory bottom-up).
onerror --optional, is a function; It is called when there is a parameter, an OSError instance. After reporting this error, continue to walk, or throw exception to terminate walk.
followlinks --set to True to access the directory through a soft link.
return value
The method has no return value.
A folder:
#建立了的文件目录 to see how the Os.walk works
# folder path, folder name, file name
>>> for Root,dirs,files in Os.walk (' D:/test '):
Print (Root,dirs,files)
d:/test ['a''b'] ['1.txt' 2.txt']d:/test\a ['aa'] [' 1.txt ' ]d:/Test\a\aa [] []d:/test\b [] []
The third item, which is the file name, helps us to compress all file operations
>>> Z=zipfile. ZipFile ('d:/tt.zip','w') for in Os.walk ('d:/test'): #获取所有文件名 for in Files: z.write (Os.path.join (root,file)) #os. path.join form the full file path name? Not very clear. 2017-11-05 >>> z.close ()
I was afraid that I might put all the files in the zip file instead of creating the corresponding folder, the result of the response to create a folder, but did not create a B folder, because B is an empty folder (using this way will not have empty folders, how to make him have it, Learn more later to solve the 2017-11-05)
Using the ZipFile module to write Python code
#Filename:backup.pyImportOs,time,zipfile#A list of files to back upSource = ['C:\\USERS\\HM\\DESKTOP\\WEB\\CH2','C:\\users\\hm\\desktop\\web\\ch3']#To construct a backup destination file.Target_dir ='D:\\backup'Target= Target_dir + os.sep + time.strftime ('%y%m%d%h%m%s')+'. zip'#use ZipFile to compress#Create a compressed package variable ZZ=zipfile. ZipFile (Target,'W')#traverse all the files forBack_dirinchSource: forRoot,filedirs,filesinchOs.walk (back_dir): forFileinchFiles:z.write (Os.path.join (root,file)) Z.close ()Print('successfully backed up to', target)
Backup Successful!
Additional improvements
Use date as folder, time as default file name, allow user to enter note name by themselves
1. If there is no today's date folder, create a
2. The user entered their own name, using the name (replace the space with _ to prevent problems in processing)
#Filename:backup.pyImportOs,time,zipfile#A list of files to back upSource = ['C:\\USERS\\HM\\DESKTOP\\WEB\\CH2','C:\\users\\hm\\desktop\\web\\ch3']#To construct a backup destination file.Target_dir ='D:\\backup'#Create a Date folderToday = Target_dir + Os.sep + time.strftime ('%y%m%d')if notos.path.exists (today): Os.mkdir (today)#Create file nameComment = input ("Please enter a comment:")ifLen (comment) = =0:target= today + Os.sep + time.strftime ('%h%m%s')+'. zip'Else: Target= today + Os.sep + time.strftime ('%h%m%s') + Comment.replace (' ','_') +'. zip'#use ZipFile to compress#Create a compressed package variable ZZ=zipfile. ZipFile (Target,'W')#traverse all the files forBack_dirinchSource: forRoot,filedirs,filesinchOs.walk (back_dir): forFileinchFiles:z.write (Os.path.join (root,file)) Z.close ()Print('successfully backed up to', target)
Python Simple backup File script v2.0