Crossin的编程教室

标题: 文件备份出错,求大神解答!! [打印本页]

作者: 益力多小姐    时间: 2019-11-28 19:07
标题: 文件备份出错,求大神解答!!
原代码:


import os
import time

#需要备份的文件
source=r'D:\陈秋霞1\001.xlsx'

#存储备份文件的位置
target_dir=r'D:\陈秋霞'

#判断文件是否存在
if not os.path.exists(target_dir):
    os.mkdir(target_dir)

today=target_dir+time.strftime('%Y-%m-%d')

#now指备份文件名字
now=time.strftime('%H%M%S')


#输入对备份文件的标签
comment=input('input comment-->')

if len(comment)==0:
    target=today+os.sep+now+'.zip'
else:
    target=today+os.sep+now+'_'+\
            comment.replace('','_')+'.zip'

#判断是否生成了名为today的文件夹
if not os.path.exists(today):
    os.mkdir(today)
    print('ok,create:',today)

zip_command=r"7z a %s %s"%(target,source)

print('zip command is:')
print('zip_command')
print('running:')

if os.system(zip_command)==0:
    print('success')
else:
    print('error')


运行结果:
input comment-->mytest
ok,create: D:\陈秋霞2019-11-28
zip command is:
zip_command
running:
error
>>>



微信图片_20191128190354.png (42.78 KB, 下载次数: 247)

微信图片_20191128190354.png


作者: crossin先生    时间: 2019-11-29 12:22
windows默认没有zip命令
是看 笨办法学python 吗?这个可以略过

想做的话,你要先给windows装上zip压缩工具并在命令行下配置好
作者: 益力多小姐    时间: 2019-11-29 14:44
crossin先生 发表于 2019-11-29 12:22
windows默认没有zip命令
是看 笨办法学python 吗?这个可以略过

谢谢




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5