python创建文件备份的脚本


Posted in Python onSeptember 11, 2018

制作文件备份

打开原文件

old_f_name = input(“请输入备份的文件路径:”) 
old_f = open(old_f_name, “r”)

打开新文件

new_f_name = “[复件]” + old_f_name 
 123.txt -> 123[复件].txt 123 + “[复件]” + .txt 
 index = old_f_name.rfind(“.”) # 获取.对应的后缀 
if index >= 0: # 如果有后缀 
new_f_name = old_f_name[:index] + “[复件]” + old_f_name[index:] 
 else: # 如果没有后缀 
new_f_name = old_f_name + “[复件]” 
new_f = open(new_f_name, “w”)

读取原文件内容

content = old_f.read()

写入到新文件中

new_f.write(content)

关闭原文件

old_f.close()

关闭新文件

new_f.close()

补充:下面看下python文件备份脚本

import os
import time
source = ['D:\\MyDrivers\hotfix']  #这里可以用自然字符串表示r',因为windows下的分隔符
与python的有冲突,所以需要转义字符\
# 2. 备份文件到目标路径
target_dir = 'F:\\DMDownLoad\\' #这里的末尾一定不要丢分隔符,否者创建的文件会在F:目录下,
而不会在DMDownload目录下
# 3. The files are backed up into a zip file.
# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d') #time.strftime表示对当前时间的调用,括号内为参数设定
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')
# Take a comment from the user to create the name of the zip file
comment = raw_input('Enter a comment -->')
if len(comment)==0: 
  target = today+os.sep+now+'.zip' 
#os.sep表示目录符号,windows下是\\,linux下是/,mac下是:,这里为了保证移植性,
所以os.sep会根据系统给出分隔符
else:
  target = today+os.sep+now+'_'+\
       comment.replace(' ','_')+'.zip'
  # Notice the backslash!
# Create the subdirectory if it isn't already there
if not os.path.exists(today):
  os.mkdir(today) # make directory
  print('Successfully created directory', today)
# 5. 用winrar的rar命令压缩文件,但首先要安装有winrar且设置winrar到环境变量的路径path中
zip_command = "rar a %s %s" %(target,''.join(source))
#这行命令之前的所有target  、target_dir、today这些都是字符串,只有在
这个命令和os.makedir中才是真正的表示路径
# Run the backup
#设置winrar到path环境中,这里已经手动添加了,如果没有去掉#号
#os.system('set Path=%Path%;C:\Program Files\WinRAR')
if os.system(zip_command)==0:
  print'Successful backup to', target
else:
  print'Backup FAILED'

总结

以上所述是小编给大家介绍的python创建文件备份的脚本,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
学习python之编写简单乘法口诀表实现代码
Feb 27 Python
Django中的CBV和FBV示例介绍
Feb 25 Python
使用python将图片按标签分入不同文件夹的方法
Dec 08 Python
Python实现的拉格朗日插值法示例
Jan 08 Python
Django实现网页分页功能
Oct 31 Python
django框架cookie和session用法实例详解
Dec 10 Python
用Python在Excel里画出蒙娜丽莎的方法示例
Apr 28 Python
使用python爬取抖音app视频的实例代码
Dec 01 Python
python中的对数log函数表示及用法
Dec 09 Python
python Zmail模块简介与使用示例
Dec 19 Python
python使用tkinter实现透明窗体上绘制随机出现的小球(实例代码)
May 17 Python
自动在Windows中运行Python脚本并定时触发功能实现
Sep 04 Python
python实现飞机大战
Sep 11 #Python
pygame实现简易飞机大战
Sep 11 #Python
python实现飞机大战微信小游戏
Mar 21 #Python
python实现微信小程序自动回复
Sep 10 #Python
python中map的基本用法示例
Sep 10 #Python
python2 与 pyhton3的输入语句写法小结
Sep 10 #Python
django DRF图片路径问题的解决方法
Sep 10 #Python
You might like
PHP form 表单传参明细研究
2009/07/17 PHP
PHP统计目录下的文件总数及代码行数(去除注释及空行)
2011/01/17 PHP
基于xcache的配置与使用详解
2013/06/18 PHP
详解PHP用substr函数截取字符串中的某部分
2016/12/03 PHP
jquery 简单图片导航插件jquery.imgNav.js
2010/03/17 Javascript
对setInterval在火狐和chrome切换标签产生奇怪的效果之探索,与解决方案!
2011/10/29 Javascript
不使用XMLHttpRequest实现异步加载 Iframe和script
2012/10/29 Javascript
caller和callee的区别介绍及演示结果
2013/03/10 Javascript
Jquery实现列表(隔行换色,全选,鼠标滑过当前行)效果实例
2013/06/09 Javascript
使用js对select动态添加和删除OPTION示例代码
2013/08/12 Javascript
微信JS接口汇总及使用详解
2015/01/09 Javascript
javascript实现Table间隔色以及选择高亮(和动态切换数据)的方法
2015/05/14 Javascript
原生js编写autoComplete插件
2016/04/13 Javascript
基于javascript实现图片切换效果
2016/04/17 Javascript
javascript中异常处理案例(推荐)
2016/10/03 Javascript
使用DeviceOne实现微信小程序功能
2016/12/29 Javascript
微信小程序之数据缓存的实例详解
2017/09/29 Javascript
继承行为在 ES5 与 ES6 中的区别详解
2019/12/24 Javascript
[01:46]TI4西雅图DOTA2前线报道 中国选手抱团调时差
2014/07/08 DOTA
[10:49]2014国际邀请赛 叨叨刀塔第二期为真正的电竞喝彩
2014/07/21 DOTA
Python中字符串的常见操作技巧总结
2016/07/28 Python
解决pandas无法在pycharm中使用plot()方法显示图像的问题
2018/05/24 Python
解决pycharm py文件运行后停止按钮变成了灰色的问题
2018/11/29 Python
python3对拉勾数据进行可视化分析的方法详解
2019/04/03 Python
Django自定义用户登录认证示例代码
2019/06/30 Python
python 实现仿微信聊天时间格式化显示的代码
2020/04/17 Python
详解Python直接赋值,深拷贝和浅拷贝
2020/07/09 Python
python中round函数保留两位小数的方法
2020/12/04 Python
MATCHESFASHION澳大利亚/亚太地区:英国时尚奢侈品电商
2020/01/14 全球购物
用C#语言写出在本地创建一个UDP接收端口的具体过程
2016/02/22 面试题
外贸业务员求职自荐信分享
2013/09/21 职场文书
人力资源管理专业应届生求职信
2014/04/24 职场文书
学校领导班子四风对照检查材料
2014/09/27 职场文书
考试没考好检讨书
2015/05/06 职场文书
2015入党个人自传范文
2015/06/26 职场文书
opencv 分类白天与夜景视频的方法
2021/06/05 Python