背景
在【简明Python教程】中介绍了Linux下的代码命令,如果要在windows上运行备份的代码,则需要安装GnuWin32,参考网上的步骤()我尝试在Win10系统进行安装,但都在运行install.bat步骤时闪退。后来了解到Windows10自带Linux系统,可能是这个原因导致最后安装 (不确定,我是技术小白)
解决方案
在网上找到一个解决方案,安装7z压缩软件
把7z的默认安装路径(C:\Program Files\7-Zip)加入系统的PATH中(需要重启一下Python),代码如下
# windows 10环境下的备份
import os
import time
# 1. 考虑到将来需要备份的文件很多,这里用有列表来存储备份的文件与目录
source = ['d:\PycharmProjects\\note.txt']
# 2. 定义备份文件必须存储的目标目录
target_dir = 'D:\\PycharmProjects\\BackupTest'
# 3. 如果目标目录还不存在,则进行创建
if not os.path.exists(target_dir):
os.mkdir(target_dir) # 创建目录
# 4. 备份文件将打包压缩成 zip 文件。并且压缩文件的文件名由当前日期与时间构成。
# 注意: os.sep 变量的使用方式——它将根据你的操作系统给出相应的分隔符,在
# GNU/Linux 与 Unix 中它会是 '/' ,在 Windows 中它会是 '\\' ,在 Mac OS 中它会是
# ':' 。使用 os.sep 而非直接使用这些字符有助于使我们的程序变得可移植
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
# 5. 我们使用 windows系统下的7z压缩软件将文件打包成 zip 格式
zip_command = "7z a %s %s" % (target, ' '.join(source))
# 运行备份
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
改进1
# windows 10环境下的备份, 将当前日期作为主备份目录下的子目录名称, 并将前时间作为zip文件的文件名
import os
import time
# 1. 考虑到将来需要备份的文件很多,这里用有列表来存储备份的文件与目录
source = ['d:\PycharmProjects\\note.txt']
# 2. 定义备份文件必须存储的目标目录
target_dir = 'D:\\PycharmProjects\\BackupTest'
# 3. 如果目标目录还不存在,则进行创建
if not os.path.exists(target_dir):
os.mkdir(target_dir) # 创建目录
# 4. 备份文件将打包压缩成 zip 文件。将当前日期作为主备份目录下的子目录名称
# 并将前时间作为zip文件的文件名
today = target_dir + os.sep + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
target = today + os.sep + now + '.zip'
# 5. 如果子目录沿不存在则创建一个
if not os.path.exists(today):
os.mkdir(today)
# 6. 我们使用 windows系统下的7z压缩软件将文件打包成 zip 格式
zip_command = "7z a %s %s" % (target, ' '.join(source))
# 运行备份
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
改进2
# windows 10环境下的备份,可以加入comment
import os
import time
# 1. 考虑到将来需要备份的文件很多,这里用有列表来存储备份的文件与目录
source = ['d:\PycharmProjects\\note.txt','d:\PycharmProjects\\testnote.txt']
# 2. 定义备份文件必须存储的目标目录
target_dir = 'D:\\PycharmProjects\\BackupTest'
# 3. 如果目标目录还不存在,则进行创建
if not os.path.exists(target_dir):
os.mkdir(target_dir) # 创建目录
# 4. 备份文件将打包压缩成 zip 文件。将当前日期作为主备份目录下的子目录名称
# 并将前时间作为zip文件的文件名
today = target_dir + os.sep + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
# 5. 添加一条来自用户的注释以创建zip文件的文件名
comment = input('Enter a comment -->')
# 检查是否有评论键入
if len(comment) == 0:
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' + \
comment.replace(' ', '_') + '.zip'
# 如果子目录尚不存在则创建一个
if not os.path.exists(today):
os.mkdir(today)
print('Successfully created directory', today)
# 6. 我们使用 windows系统下的7z压缩软件将文件打包成 zip 格式
zip_command = "7z a %s %s" % (target, ' '.join(source))
# 运行备份
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
改进3
# windows 10环境下的备份,可以加入comment,可以从sys.argv获取备份列表
import os
import time
import sys
# 1. 考虑到将来需要备份的文件很多,这里用有列表来存储备份的文件与目录
source = ['d:\PycharmProjects\\note.txt']
source.extend(sys.argv)
# 2. 定义备份文件必须存储的目标目录
target_dir = 'D:\\PycharmProjects\\BackupTest'
# 3. 如果目标目录还不存在,则进行创建
if not os.path.exists(target_dir):
os.mkdir(target_dir) # 创建目录
# 4. 备份文件将打包压缩成 zip 文件。将当前日期作为主备份目录下的子目录名称
# 并将前时间作为zip文件的文件名
today = target_dir + os.sep + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
# 5. 添加一条来自用户的注释以创建zip文件的文件名
comment = input('Enter a comment -->')
# 检查是否有评论键入
if len(comment) == 0:
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' + \
comment.replace(' ', '_') + '.zip'
# 如果子目录尚不存在则创建一个
if not os.path.exists(today):
os.mkdir(today)
print('Successfully created directory', today)
# 6. 我们使用 windows系统下的7z压缩软件将文件打包成 zip 格式
zip_command = "7z a %s %s" % (target, ' '.join(source))
# 运行备份
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
改进4
# windows 10环境下的备份,可以加入comment,可以从sys.argv获取备份列表
# 用zipfile模块而非os.system来创建备份
import os
import time
import sys
import zipfile
# 1. 考虑到将来需要备份的文件很多,这里用有列表来存储备份的文件与目录
source = 'd:\PycharmProjects\\note.txt'
sourcefiles = ['note.txt', 'testnote.txt']
# source.extend(sys.argv)
# 2. 定义备份文件必须存储的目标目录
target_dir = 'D:\\PycharmProjects\\BackupTest'
# 3. 如果目标目录还不存在,则进行创建
if not os.path.exists(target_dir):
os.mkdir(target_dir) # 创建目录
# 4. 备份文件将打包压缩成 zip 文件。将当前日期作为主备份目录下的子目录名称
# 并将前时间作为zip文件的文件名
today = target_dir + os.sep + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
# 5. 添加一条来自用户的注释以创建zip文件的文件名
comment = input('Enter a comment -->')
# 检查是否有评论键入
if len(comment) == 0:
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' + \
comment.replace(' ', '_') + '.zip'
# 如果子目录尚不存在则创建一个
if not os.path.exists(today):
os.mkdir(today)
print('Successfully created directory', today)
# 6. 我们使用 windows系统下的7z压缩软件将文件打包成 zip 格式
zip_command = "7z a %s %s" % (target, ' '.join(source))
# 运行备份
f = zipfile.ZipFile(target, 'w', zipfile.ZIP_DEFLATED)
for sourcefile in sourcefiles:
f.write(source, sourcefile)
f.close()