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获取Linux下文件版本信息、公司名和产品名的方法
Oct 05 Python
Python制作Windows系统服务
Mar 25 Python
python构建深度神经网络(续)
Mar 10 Python
在pandas中一次性删除dataframe的多个列方法
Apr 10 Python
python+mysql实现学生信息查询系统
Feb 21 Python
Python异常处理例题整理
Jul 07 Python
Python 实现将数组/矩阵转换成Image类
Jan 09 Python
Python下利用BeautifulSoup解析HTML的实现
Jan 17 Python
使用IPython或Spyder将省略号表示的内容完整输出
Apr 20 Python
对Keras中predict()方法和predict_classes()方法的区别说明
Jun 09 Python
利用Pycharm连接服务器的全过程记录
Jul 01 Python
Python类方法总结讲解
Jul 26 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程序时遇到的三个问题
2012/01/17 PHP
PHP-FPM和Nginx的通信机制详解
2019/02/01 PHP
网上应用的一个不错common.js脚本
2007/08/08 Javascript
js split 的用法和定义 js split分割字符串成数组的实例代码
2012/05/13 Javascript
利用jQuery的deferred对象实现异步按顺序加载JS文件
2013/03/17 Javascript
Javascript页面添加到收藏夹的简单方法
2013/08/07 Javascript
基于js与flash实现的网站flv视频播放插件代码
2014/10/14 Javascript
JavaScript引用类型和基本类型详解
2016/01/06 Javascript
几种二级联动案例(jQuery\Array\Ajax php)
2016/08/13 Javascript
jQuery实现一个简单的轮播图
2017/02/19 Javascript
Angular 2 ngForm中的ngModel、[ngModel]和[(ngModel)]的写法
2017/06/29 Javascript
打字效果动画的4种实现方法(超简单)
2017/10/18 Javascript
JS获取当前地理位置的方法
2017/10/25 Javascript
javascript用rem来做响应式开发
2018/01/13 Javascript
JS中移除非数字最多保留一位小数
2018/05/09 Javascript
JS伪继承prototype实现方法示例
2018/06/20 Javascript
使用Vue 自定义文件选择器组件的实例代码
2020/03/04 Javascript
js实现抽奖的两种方法
2020/03/19 Javascript
jQuery中getJSON跨域原理的深入讲解
2020/09/02 jQuery
python实现百度关键词排名查询
2014/03/30 Python
python之PyMongo使用总结
2017/05/26 Python
浅谈Django自定义模板标签template_tags的用处
2017/12/20 Python
Python实现二维数组输出为图片
2018/04/03 Python
Python实现的个人所得税计算器示例
2018/06/01 Python
解决Python print输出不换行没空格的问题
2018/11/14 Python
python文件写入write()的操作
2019/05/14 Python
python语言线程标准库threading.local解读总结
2019/11/10 Python
python GUI库图形界面开发之PyQt5表单布局控件QFormLayout详细使用方法与实例
2020/03/06 Python
python+selenium 脚本实现每天自动登记的思路详解
2020/03/11 Python
利用Python函数实现一个万历表完整示例
2021/01/23 Python
Jimmy Choo美国官网:周仰杰鞋子品牌
2018/06/08 全球购物
澳大利亚排名第一的露营和户外设备在线零售商:Outbax
2020/05/06 全球购物
后勤副校长自我鉴定
2013/10/13 职场文书
创先争优演讲稿
2014/09/15 职场文书
2014年个人师德工作总结
2014/12/04 职场文书
导游词之嵊泗列岛
2019/10/30 职场文书