Holistic approach
List the directories you want to back up, compressing them, and backing them up by executing system commands.
The key is to construct the command and use Os.system () to execute, the first use of the ZIP command has not been successful, and later found that Windows does not have this command, but also to install the GnuWin32 project, later installed 7z, implemented by using system commands for compression.
Compress command
通过下载7z压缩,将7z.exe 7z,dll 加入系统环境变量目录,通过以下命令进行压缩、解压
7z a test.zip a.txt b.txt # 指定若干文件7z a test.zip f:/test/** # 压缩文件夹
7z x test.zip -of:\test # -o表示输出目录,注意其与目录路径之间没有空格7z x test.zip -o"f:\test abc" # 假如输出文件夹有空格,用引号包裹
测试一下:
C:\users\hm\desktop\web>7z a tes.7z python362.chm7-zip17.01Beta (x64): Copyright (c)1999- .Igor Pavlov: .- ,- -scanning The drive:1File8010498Bytes (7823KiB) Creating Archive:tes.7zaddNewData to archive:1File8010498Bytes (7823KiB) Files Read fromDisk1Archive Size:7982833Bytes (7796KiB) Everything isOk
Writing Python backup code
After the 7z command compression is complete, start writing the python Backup code:
#Filename:backup.pyImportOs,time#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')+'. 7z'
#strftime规划了时间的格式, here's a closer look at this function
#constructing the system command lineCommand ="7z a {0} {1}". Format (Target,' '. Join (source))#Do strings appear in double quotes? #Print (command)
#using the System execution command, the execution succeeds will return 0ifOs.system (command) = =0:Print('Backup succeeded to', Target)Else: Print('Backup Failed')
First encountered this error, because the path uses the ' C:\user ' representation method
SyntaxError
---------------------------
(Unicode error) ' Unicodeescape ' codec can ' t decode bytes in position 2-3: truncated \uxxxxxxxx escape
The reason for this error is that "\" in the code means escape in Python.
1. Add R or R before the string, i.e.:
Source = [R'C:\Users\hm\Desktop\Web\CH2', R'C:\Users\hm\Desktop\Web\CH3 ']
, where R or R represents a string that is not escaped in Python.
2, "\" before adding "\" to achieve escape.
3. Change "\" to "/",
Correct the wrong backup success
Backup succeeded to D:\backup\20171105151908.7z
About the STRFTIMR function
Check the function data as follows:
Grammar
Strftime () method syntax:
Time. Strftime(format[, T])
Parameters
- Format--formatting string.
- T--optional parameter T is a Struct_time object.
Python format symbols in time Date:
- %y Two-digit year representation (00-99)
- %Y Four-digit year representation (000-9999)
- %m Month (01-12)
- One day in%d months (0-31)
- %H 24-hour hours (0-23)
- %I 12-hour hours (01-12)
- %M minutes (00=59)
- %s seconds (00-59)
- %a Local Simplified Week name
- %A Local Full week name
- %b a locally simplified month name
- %B Local Full month name
- %c Local corresponding date representation and time representation
- %j Day of the Year (001-366)
- %p the equivalent of a local a.m. or p.m.
- %u weeks of the year (00-53) Sunday is the beginning of the week
- %w Week (0-6), Sunday for the beginning of the week
- %W Week of the Year (00-53) Monday is the beginning of the week
- %x Local corresponding date representation
- %x Local corresponding time representation
- %Z the name of the current time zone
- Percent% of the number itself
Try this in Python:
>>>Import Time>>>Print(Time.strftime ('%b%a')) Novsun>>>Print(Time.strftime ('%b%a'), Sun, Nov>>>Print(Time.strftime ('%b%a%Z') Nov Sun China Standard Time>>>Print(Time.strftime ('%b%a%p') (Sun PM )
Python Simple backup File script