简明python教程第十章为实现压缩文件夹保存备份的方案,提供了如下代码:
#!/usr/bin/python
# Filename: backup_ver1.py
import os
import time
# 1. The files and directories to be backed up are
specified in a list.
source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source =
[r'C:\Documents', r'D:\Work'] or something like that
# 2. The backup must be stored in a main backup
directory
target_dir = '/mnt/e/backup/' # Remember to change this
to what you will be using
# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date
and time
target = 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 archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join
(source))
# Run the backup
if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
print 'Backup FAILED'
该方案使用zip处理压缩文件,在linux环境下正常输出:
$ python backup_ver1.py
Successful backup to /mnt/e/backup/20041208073244.zip
然而在windows 10系统,python2.7.9环境下会报错,未安装zip
'zip' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
考虑到windows下 zip支持环境,我改用7z实现解压缩。并对windows和linux环境同时支持:
import platform
platform.system()
# 输出结果为当前平台信息,用以做判断依据。
遂有:
# coding=utf-8
# -*- coding: utf-8 -*-
__author__ = 'nanguoyu'
import os
import time
import platform
# 备份目标文件夹
source_windows = 'C:\\Users\\nanguoyu\\OneDrive\\文档\\获奖证书\\'
source_linux = '/mnt/c/Users/nanguoyu/OneDrive/文档/获奖证书/'
# 备份存储文件夹
tar_dir_win = 'F:\\Dropbox\\'
tar_dir_nux = '/mnt/f/Dropbox/'
tips = '备份'
if platform.system() == 'Windows':
print("当前系统windows")
target_dir = tar_dir_win
source = source_windows
zsource = source + '*'
target7z = target_dir + time.strftime('%Y%m%d') + tips + os.sep + time.strftime('%H%M%S') + '.zip'
zcommand = "7z a -tzip -r %s %s" % (target7z, ''.join(zsource))
elif platform.system() == 'Linux':
print("当前系统linux")
target_dir = tar_dir_nux
source = source_linux
zsource = source + '*'
target7z = target_dir + time.strftime('%Y%m%d') + tips + os.sep + time.strftime('%H%M%S') + '.zip'
zcommand = "7z a -tzip -r %s %s" % (target7z, ''.join(zsource))
else:
print("当前系统linux")
target_dir = tar_dir_nux
source = source_linux
zsource = source + '*'
target7z = target_dir + time.strftime('%Y%m%d') + tips + os.sep + time.strftime('%H%M%S') + '.zip'
zcommand = "7z a -tzip -r %s %s" % (target7z, ''.join(zsource))
print ('zcommand is : ', zcommand)
if os.system(zcommand) == 0:
print ('successful backup to ', target7z)
else:
print ('Backup Failed')
# end of file
windows下输出:
E:\workplace\python_begin\Backup>python BackupToDropbox.py
当当前前系系统统windows
zcommand is : 7z a -tzip -r F:\Dropbox\20160601备备份份\234838.zip C:\Users\nanguoyu\OneDrive\文文档档\获获奖奖证证书书\*
7-Zip [32] 16.00 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-10
Scanning the drive:
1 file, 206237 bytes (202 KiB)
Creating archive: F:\Dropbox\20160601备备份份\234838.zip
Items to compress: 1
Files read from disk: 1
Archive size: 178257 bytes (175 KiB)
Everything is Ok
successful backup to F:\Dropbox\20160601备备份份\234838.zip