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判断质数(素数)的简单方法讲解
May 05 Python
整理Python 常用string函数(收藏)
May 30 Python
详解python数据结构和算法
Apr 18 Python
Python 抓取微信公众号账号信息的方法
Jun 14 Python
在python 中split()使用多符号分割的例子
Jul 15 Python
Python turtle库绘制菱形的3种方式小结
Nov 23 Python
Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)
Jan 03 Python
python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法
Nov 05 Python
2020年10款优秀的Python第三方库,看看有你中意的吗?
Jan 12 Python
python中random模块详解
Mar 01 Python
如何用Python搭建gRPC服务
Jun 30 Python
Python Pygame实战之塔防游戏的实现
Mar 17 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学习之PHP变量
2006/10/09 PHP
三个类概括PHP的五种设计模式
2012/09/05 PHP
PHP中Memcache操作类及用法实例
2014/12/12 PHP
php接口技术实例详解
2016/12/07 PHP
Javascript 判断客户端浏览器类型代码
2010/03/01 Javascript
javascript使用onclick事件改变选中行的颜色
2013/12/30 Javascript
js实现按钮颜色渐变动画效果
2015/08/20 Javascript
vue+iview 兼容IE11浏览器的实现方法
2019/01/07 Javascript
js实现旋转木马轮播图效果
2020/01/10 Javascript
[03:57]2016完美“圣”典风云人物:rOtk专访
2016/12/09 DOTA
[49:30]DOTA2-DPC中国联赛正赛 Dragon vs Dynasty BO3 第二场 3月4日
2021/03/11 DOTA
python批量修改文件名的实现代码
2014/09/01 Python
python中偏函数partial用法实例分析
2015/07/08 Python
用Python编写简单的微博爬虫
2016/03/04 Python
python 计算两个日期相差多少个月实例代码
2017/05/24 Python
Python计时相关操作详解【time,datetime】
2017/05/26 Python
Python实现的维尼吉亚密码算法示例
2018/04/12 Python
python排序函数sort()与sorted()的区别
2018/09/18 Python
关于Django ForeignKey 反向查询中filter和_set的效率对比详解
2018/12/15 Python
python重试装饰器的简单实现方法
2019/01/31 Python
Python利用heapq实现一个优先级队列的方法
2019/02/03 Python
python模块常用用法实例详解
2019/10/17 Python
Windows10下Tensorflow2.0 安装及环境配置教程(图文)
2019/11/21 Python
Anaconda3中的Jupyter notebook添加目录插件的实现
2020/05/18 Python
python判断字符串以什么结尾的实例方法
2020/09/18 Python
Python实现随机爬山算法
2021/01/29 Python
非常震撼的纯CSS3人物行走动画
2016/02/24 HTML / CSS
Free People中国官网:波西米亚风格女装服饰
2016/08/30 全球购物
Clos19英国:高档香槟、葡萄酒和烈酒在线购物平台
2020/07/10 全球购物
C/C++ 笔试、面试题目大汇总
2015/11/21 面试题
AJax面试题
2014/11/25 面试题
元旦红领巾广播稿
2014/02/19 职场文书
副科竞争上岗演讲稿
2014/05/12 职场文书
我的兄弟姐妹观后感
2015/06/15 职场文书
运动会5000米加油稿
2015/07/21 职场文书
导游词之南迦巴瓦峰
2019/11/19 职场文书