在 Python 中使用 7zip 备份文件

在 Python 中使用 7zip 备份文件

我在按照 Byte of python一步步的学习Python, 在学到‘解决方案’的时候,原文的实例 “backup_ver1.py” 是用zip备份文件。

这里面我有几点不一样的地方:

  • 我的电脑没有zip,我用的是7zip;
  • 原文直接用‘zip’命令备份,我直接使用7z命令报错。

 

使用7z命令备份之前,需要把7zip的安装目录添加到系统环境变量Path中;这时候我可以在CMD中执行7z,但是在python中还是报错,“7z is not recognized as an internal ……”

下面三种方法可以在python中正确运行7z命令:

# 方法1: 拷贝 7z.exe 和7z.dll 到当前python文件所在的目录下。 否则,不认识7z 命令。
zip_command = '7z a -tzip {0} {1} -r'.format(target, ' '.join(source))

# 方法2: os.system() 里面执行的是同目录下的exe, 使用如下os.chdir() 命令切换 path。
os.chdir('D:\\Program Files (x86)\\7-Zip')
print('切换当前路径为:', os.getcwd())
zip_command = '7z a -tzip {0} {1} -r'.format(target, ' '.join(source))

# 方法3:在cmd 命令中写入7z.exe所在的目录
zip_command = '"D:\\Program Files (x86)\\7-Zip\\7z.exe" a -tzip {0} {1} '.format(target, ' '.join(source))

import os
import time

# 1. 需要备份的文件与目录将被指定在一个列表中。
# 例如在 Windows 下:source = ['"C:\\My Documents"', 'C:\\Code']
# 又例如在 Mac OS X 与 Linux 下:source = ['/Users/swa/notes']
source = ['D:\\test\\fold\\']
# 在这里要注意到我们必须在字符串中使用双引号用以括起其中包含空格的名称。

# 2. 备份文件必须存储在一个主备份目录中
# 例如在 Windows 下:target_dir = 'E:\\Backup'
# 又例如在 Mac OS X 和 Linux 下:target_dir = '/Users/swa/backup'
target_dir = 'D:\\test\\Backup'
# 要记得将这里的目录地址修改至你将使用的路径

# 3. 备份文件将打包压缩成 zip 文件。
# 4. zip 压缩文件的文件名由当前日期与时间构成。
# os.sep 变量的使用方式——它将根据你的操作系统给出相应的分隔符,在# GNU/Linux 与 Unix 中它会是 '/' ,在 Windows 中它会是 '\\' ,在 Mac OS 中它会是 ':'
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'

# 如果目标目录还不存在,则进行创建
if not os.path.exists(target_dir):
    os.mkdir(target_dir)

# 5. 我们使用 zip 命令将文件打包成 zip 格式
# 方法1: 拷贝 7z.exe 和7z.dll 到当前python文件所在的目录下。 否则,不认识7z 命令。
# zip_command = '7z a -tzip {0} {1} -r'.format(target, ' '.join(source))

# 方法2: os.system() 里面执行的是同目录下的exe, 使用如下os.chdir() 命令切换 path。
# os.chdir('D:\\Program Files (x86)\\7-Zip')
# print('切换当前路径为:', os.getcwd())
# zip_command = '7z a -tzip {0} {1} -r'.format(target, ' '.join(source))

# 方法3:在cmd 命令中写入7z.exe所在的目录
# -mcu 强制使用utf-8 编码文件名
zip_command = '"D:\\Program Files (x86)\\7-Zip\\7z.exe" a -tzip -mcu {0} {1} '.format(target, ' '.join(source))
# 运行备份
print('\nZip command is:')
print(zip_command)
print('Running:')


if os.system(zip_command) == 0:
    print('Successful backup to', target)
else:
    print('Backup FAILED')

# 查看压缩文件内容
check_command = '"D:\\Program Files (x86)\\7-Zip\\7z.exe" l {0}'.format(target)

print('\nCheck zipfile command is:')
print(check_command)
print('Running:')

# 使用 os.system(check_command) 中文返回有乱码,所以使用 os.popen
# if os.system(check_command) == 0:
#     print('Please check the file list in:', target)
# else:
#     print('Check info FAILED')
print('Please check the file list in:', target)
p = os.popen(check_command)
print(p.read())
p.close()


# 解压缩到目录
extr_command = '"D:\\Program Files (x86)\\7-Zip\\7z.exe" x  {0} -oD:\\test\\extract\\ -y'.format(target)

print('\nExtract command is:')
print(extr_command)
print('Running:')

if os.system(extr_command) == 0:
    print('Successful extract to', 'D:\\test\\extract')
else:
    print('Extract FAILED')

 

注意:

在压缩的时候,不要使用 -r,递归会把folder同级的其它目录下的文件一起压缩;

在解压的时候,使用-y,如果当前目录下已存在被解压的目录和文件,替换目标文件。

 

zip_command = '"D:\\Program Files (x86)\\7-Zip\\7z.exe" a -tzip {0} {1} -r'.format(target, ' '.join(source))

extr_command = '"D:\\Program Files (x86)\\7-Zip\\7z.exe" x  {0} -oD:\\test\\extract\\ -y'.format(target)
 
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值