一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

在Python中使用7zip备份文件代码示例

时间:2020-12-11 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下在Python中使用7zip备份文件代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

使用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)

热门栏目