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通过urllib2爬网页上种子下载示例
Feb 24 Python
八大排序算法的Python实现
Jan 28 Python
Python注释详解
Jun 01 Python
python通过elixir包操作mysql数据库实例代码
Jan 31 Python
Django中间件工作流程及写法实例代码
Feb 06 Python
关于Python正则表达式 findall函数问题详解
Mar 22 Python
Python批量生成幻影坦克图片实例代码
Jun 04 Python
python 字符串常用方法汇总详解
Sep 16 Python
python 使用pygame工具包实现贪吃蛇游戏(多彩版)
Oct 30 Python
python opencv实现gif图片分解的示例代码
Dec 13 Python
Python实战之疫苗研发情况可视化
May 18 Python
Python利用FlashText算法实现替换字符串
Mar 31 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实现两个数组相加的方法
2015/02/17 PHP
php技巧小结【推荐】
2017/01/19 PHP
Vue.directive()的用法和实例详解
2018/03/04 Javascript
webpack+vue-cli项目中引入外部非模块格式js的方法
2018/09/28 Javascript
VUE+Element环境搭建与安装的方法步骤
2019/01/24 Javascript
vue2.0实现的tab标签切换效果(内容可自定义)示例
2019/02/11 Javascript
基于Vue的商品主图放大镜方案详解
2019/09/19 Javascript
React实现类似淘宝tab居中切换效果的示例代码
2020/06/02 Javascript
Postman内建变量常用方法实例解析
2020/07/28 Javascript
javascript实现时间日期的格式化的方法汇总
2020/08/06 Javascript
vue 验证两次输入的密码是否一致的方法示例
2020/09/29 Javascript
python3.5使用tkinter制作记事本
2016/06/20 Python
Anaconda下安装mysql-python的包实例
2018/06/11 Python
python中将两组数据放在一起按照某一固定顺序shuffle的实例
2019/07/15 Python
python实现证件照换底功能
2019/08/20 Python
Python图像处理库PIL的ImageEnhance模块使用介绍
2020/02/26 Python
Python分析微信好友性别比例和省份城市分布比例的方法示例【基于itchat模块】
2020/05/29 Python
python和php哪个容易学
2020/06/19 Python
Python类绑定方法及非绑定方法实例解析
2020/10/09 Python
python 爬虫如何实现百度翻译
2020/11/16 Python
HTML5拖拽API经典实例详解
2018/04/20 HTML / CSS
20世纪40年代连衣裙和复古服装:The Seamstress Of Bloomsbury
2018/07/24 全球购物
企业项目策划书
2014/01/11 职场文书
迅雷Cued工作心得体会
2014/01/27 职场文书
医院院务公开实施方案
2014/05/03 职场文书
学习作风建设心得体会
2014/10/22 职场文书
学校党的群众路线教育实践活动总结材料
2014/10/30 职场文书
博士生专家推荐信
2015/03/25 职场文书
2015年派出所民警工作总结
2015/04/24 职场文书
2015年高中班主任工作总结
2015/04/30 职场文书
基层党支部承诺书
2015/04/30 职场文书
撤诉申请怎么写
2015/05/19 职场文书
大学生自我鉴定怎么写
2019/05/07 职场文书
关于Python OS模块常用文件/目录函数详解
2021/07/01 Python
Django对接elasticsearch实现全文检索的示例代码
2021/08/02 Python
深入解析MySQL索引数据结构
2021/10/16 MySQL