Learning Python encountered a backup problem, which is documented below.
Use Python to back up files under Windows.
There are a few places to be aware of:
1. Need to download 7 zip:http://www.7-zip.org/download.html to download the corresponding version according to your own system
2. The folder name in the 7 Zip installation path cannot appear spaces, in my case the default is installed under C:\Program Files.
The program Files folder has spaces that cause the import to fail, and you can replace them as needed when you install them. (My installation path: C:\7-ZIP)
#!/usr/bin/python# Filename:backup.pyimport osimport time# 1. The files and directories to is backed up is specified in a list.source = [R ' C:\Python34\Doc ']# If you are using Windows, Use Source = [R ' C:\Documents ', R ' D:\Work ') or something like that# 2. The backup must stored in a main backup Directorytarget_dir = R ' D:\Myback\\ ' # Remember Be using# 3. The files is backed up into a zip file.# 4. The name of the ZIP archive is the current date and Timetarget = Target_dir + time.strftime ('%y%m%d%h%m%s ') + '. Zip ' # 5. We Use the Zip command (in unix/linux) to put the files in a zip Archivezip_command = r "C:\7-Zip\7z.exe A%s%s"% (target , ". Join (source)) # Run the Backupprint (' Zip_command: ', Zip_command) if Os.system (zip_command) = = 0: print (' Success Ful backup to ', Target ' else: print (' Backup FAILED ')
Python Backup Files windows