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查找目录下指定扩展名的文件实例
Apr 01 Python
python实现爬取千万淘宝商品的方法
Jun 30 Python
python中input()与raw_input()的区别分析
Feb 27 Python
Python的Flask框架及Nginx实现静态文件访问限制功能
Jun 27 Python
Python判断某个用户对某个文件的权限
Oct 13 Python
python字符串过滤性能比较5种方法
Jun 22 Python
Python中的探索性数据分析(功能式)
Dec 22 Python
python监测当前联网状态并连接的实例
Dec 18 Python
django如何实现视图重定向
Jul 24 Python
Python标准库shutil模块使用方法解析
Mar 10 Python
在spyder IPython console中,运行代码加入参数的实例
Apr 20 Python
使用Python将图片转正方形的两种方法实例代码详解
Apr 29 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
星际争霸中的热键
2020/03/04 星际争霸
优化NFR之一 --MSSQL Hello Buffer Overflow
2006/10/09 PHP
PHP中操作ini配置文件的方法
2013/04/25 PHP
CentOS下PHP7的编译安装及MySQL的支持和一些常见问题的解决办法
2015/12/17 PHP
php解决DOM乱码的方法示例代码
2016/11/20 PHP
javascript网页关闭时提醒效果脚本
2008/10/22 Javascript
IE8 chrome中table隔行换色解决办法
2010/07/09 Javascript
asm.js使用示例代码
2013/11/28 Javascript
jquery mobile页面跳转后样式丢失js失效的解决方法
2014/09/06 Javascript
再探JavaScript作用域
2014/09/24 Javascript
javascript运动详解
2015/07/06 Javascript
jQuery实现点击小图片淡入淡出显示大图片特效
2015/09/09 Javascript
老生常谈javascript变量的命名规范和注释
2016/09/29 Javascript
JS实现控制图片显示大小的方法【图片等比例缩放功能】
2017/02/18 Javascript
不得不看之JavaScript构造函数及new运算符
2017/08/21 Javascript
vue2.0 element-ui中el-select选择器无法显示选中的内容(解决方法)
2018/08/24 Javascript
浅谈Vue CLI 3结合Lerna进行UI框架设计
2019/04/14 Javascript
webpack打包优化的几个方法总结
2020/02/10 Javascript
[01:33:07]VGJ.T vs Newbee Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
python简单实现基数排序算法
2015/05/16 Python
Python实现的使用telnet登陆聊天室实例
2015/06/17 Python
Python的Twisted框架上手前所必须了解的异步编程思想
2016/05/25 Python
Python使用Windows API创建窗口示例【基于win32gui模块】
2018/05/09 Python
Python如何基于rsa模块实现非对称加密与解密
2020/01/03 Python
Python3 io文本及原始流I/O工具用法详解
2020/03/23 Python
Python写捕鱼达人的游戏实现
2020/03/31 Python
PythonPC客户端自动化实现原理(pywinauto)
2020/05/28 Python
实例代码讲解Python 线程池
2020/08/24 Python
Python系统公网私网流量监控实现流程
2020/11/23 Python
欧洲最大的美妆零售网站:Feelunique
2017/01/14 全球购物
奥巴马连任演讲稿
2014/05/15 职场文书
2014年医德医风工作总结
2014/11/13 职场文书
教代会闭幕词
2015/01/28 职场文书
2016党员学习作风建设心得体会
2016/01/21 职场文书
学前班教学反思
2016/02/24 职场文书
销售会议开幕词
2016/03/04 职场文书