


Python培训
400-996-5531
工作中,有些重要文件需要每天备份以防丢失,这里给大家展示用python一键实现备份文件到指定目录。
--------------------------------------------------------------------------------------------------------
步骤
安装python3.0以上版本
将python添加在windows环境变量中C:\Users\Acer\AppData\Local\Programs\Python\Python36\
打开python IDEL编写代码
给文件改个小图标,更美观
运行
我要备份这个文件E:\Desktop\stocks\上海、江苏库存管理.xlsx
运行效果
源码部分
import os
import time
source=[r'E:\Desktop\stocks\上海、江苏库存管理.xlsx']#需要备份的文件地址
target_dir=r'E:\库存备份/'#指定目录地址
today=target_dir+time.strftime('%Y%m%d')
now=time.strftime('H%M%S')#获取系统时间年月日
comment=input('Enter a comment-->')#输入文件名
if len(comment)==0:
target=today+os.sep+now+'.rar'#目标名称
else:
target=today+os.sep+comment.replace(' ','_')+'.rar'
if not os.path.exists(today):
os.mkdir(today)
print('Successful creat directory',today)
rar_command="WinRAR a %s %s"%(target,source[0])
if os.system(rar_command)==0:
print('Successful backup to',target)
else:
print('Backup failed')
填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!
Copyright © 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有