info-zip下載地址:Info-ZIP's SourceForge site. http://sourceforge.net/projects/infozip/files/
如下圖所示,下載紅線圈住的內容
下載完畢後安裝,記住安裝目錄,安裝完成後打開安裝目錄下的Wiz.exe,目的是執行下面代碼中zip_command。(window中沒有Unix下的zip命令,所以要下載info_zip代替)
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import
os
import
time
#1.需要備份的文件或者文件夾
source
=
[
'd:\\sll'
,
'D:\\download'
] #需要注意python下\爲轉義字符,所以要寫\\
#2.需要備份的存放目錄
target_dir
=
'd:\\pyback\\'
#3要壓縮的文件
target
=
target_dir
+
time.strftime(
'%Y%m%d%H%M%S'
)
+
'.zip'
print
target
zip_command
=
"zip -qr %s%s"
%
(target,
''
.join(source))
print
zip_command
if
os.system(zip_command)
=
=
0
:
print
'Successfull backto'
,target
else
:
print
'backup failed'
|
同時需要注意的是這裏的壓縮文件命令zip_command第一個%S 不需要加上單引號('%s'),
XXX zip_command
=
"zip -qr '%s' %s"
%
(target,
''
.join(source))這是錯誤的
這樣windows下無法創建文件。
zip_command
=
"zip -qr %s %s"
%
(target,
''
.join(source))這是正確的
然後運行 「python back.py」壓縮成功info-zip