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模拟百度登录实例详解
Jan 20 Python
Python获取指定字符前面的所有字符方法
May 02 Python
浅谈解除装饰器作用(python3新增)
Oct 15 Python
Python中安装easy_install的方法
Nov 18 Python
Python数据可视化库seaborn的使用总结
Jan 15 Python
python实现读取excel文件中所有sheet操作示例
Aug 09 Python
Python实现bilibili时间长度查询的示例代码
Jan 14 Python
django正续或者倒序查库实例
May 19 Python
Keras - GPU ID 和显存占用设定步骤
Jun 22 Python
python 怎样进行内存管理
Nov 10 Python
python使用pywinauto驱动微信客户端实现公众号爬虫
May 19 Python
python装饰器代码解析
Mar 23 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
fleaphp下不确定的多条件查询的巧妙解决方法
2008/09/11 PHP
PHP执行linux系统命令的常用函数使用说明
2010/04/27 PHP
php 删除一个数组中的某个值.兼容多维数组!
2012/02/18 PHP
ThinkPHP令牌验证实例
2014/06/18 PHP
CodeIgniter框架钩子机制实现方法【hooks类】
2018/08/21 PHP
扩展easyui.datagrid,添加数据loading遮罩效果代码
2010/11/02 Javascript
javascript验证上传文件的类型限制必须为某些格式
2013/11/14 Javascript
javascript根据时间生成m位随机数最大13位
2014/10/30 Javascript
JavaScript中的console.assert()函数介绍
2014/12/29 Javascript
jQuery选择器之子元素过滤选择器
2017/09/28 jQuery
vue项目中添加单元测试的方法
2018/07/21 Javascript
mpvue跳转页面及注意事项
2018/08/03 Javascript
json数据传到前台并解析展示成列表的方法
2018/08/06 Javascript
vue 之 css module的使用方法
2018/12/04 Javascript
详解50行代码,Node爬虫练手项目
2019/04/22 Javascript
解决VUE项目使用Element-ui 下拉组件的验证失效问题
2020/11/07 Javascript
[01:00:30]完美世界DOTA2联赛循环赛 Inki vs Matador BO2第二场 10.31
2020/11/02 DOTA
Python和perl实现批量对目录下电子书文件重命名的代码分享
2014/11/21 Python
python链接Oracle数据库的方法
2015/06/28 Python
python 日志增量抓取实现方法
2018/04/28 Python
python 重定向获取真实url的方法
2018/05/11 Python
django-filter和普通查询的例子
2019/08/12 Python
Python字典深浅拷贝与循环方式方法详解
2020/02/09 Python
Python pyautogui模块实现鼠标键盘自动化方法详解
2020/02/17 Python
使用python批量修改XML文件中图像的depth值
2020/07/22 Python
Python3如何在服务器打印资产信息
2020/08/27 Python
Scrapy基于scrapy_redis实现分布式爬虫部署的示例
2020/09/29 Python
AVI-8手表美国官方商店:AVI-8 USA
2019/04/10 全球购物
高校教师思想汇报
2014/01/11 职场文书
逃课检讨书怎么写
2015/01/01 职场文书
成本会计岗位职责
2015/02/03 职场文书
2015年体育部工作总结
2015/04/02 职场文书
呼啸山庄读书笔记
2015/06/29 职场文书
高二化学教学反思
2016/02/22 职场文书
2019各种保证书范文
2019/06/24 职场文书
Python中使用tkFileDialog实现文件选择、保存和路径选择
2022/05/20 Python