最近在自学python,教材看的是《简明Python教程》A Byte of Python网址:
http://www.kuqin.com/abyteofpython_cn/index.html
在第十章10.1版本一的代码中有使用zip的command line进行文件夹的压缩操作,
网站:http://stahlworks.com/dev/index.php?tool=zipunzip
点击页面中的zip.exe here下载zip.exe后设置Path环境变量后:
Python3.7
代码如下(根据Windows系统稍作修改):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = [r'D:\temp', 'D:\\temp2']
# 2. The backup must be stored in a main backup directory
target_dir = 'D:\\Backup\\'
# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current data and time
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
# 5. We use the zip command to put the files in a zip archive
zip_command = "zip -qr %s %s" % (target, ' '.join(source))
print(zip_command)
# Run the backup
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')