python创建文件备份的脚本

制作文件备份

打开原文件

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创建文件备份的脚本,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • python实现分析apache和nginx日志文件并输出访客ip列表的方法

    本文实例讲述了python实现分析apache和nginx日志文件并输出访客ip列表的方法.分享给大家供大家参考.具体如下: 这里使用python分析apache和nginx日志文件输出访客ip列表 ips = {} fh = open("/var/log/nginx/access.log", "r").readlines() for line in fh: ip = line.split(" ")[0] if 6 < len(ip) &l

  • python 从远程服务器下载日志文件的程序

    复制代码 代码如下: import osimport sysimport ftplibimport socket ################################################################### sign in the ftp server and download the log file. # 登陆生产服务器下载日志##############################################################

  • Python解析nginx日志文件

    项目的一个需求是解析nginx的日志文件. 简单的整理如下: 日志规则描述 首先要明确自己的Nginx的日志格式,这里采用默认Nginx日志格式: log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_f

  • python 实现创建文件夹和创建日志文件的方法

    一.实现创建文件夹和日志 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author: nulige import os import datetime #获取系统时间 log_path_suffix = datetime.datetime.now().strftime('%Y-%m-%d ') #创建文件夹 folder_name = '\log' root_directory = 'D:\python\disk_monitor' try: os

  • python使用循环实现批量创建文件夹示例

    代码很简单,其中用到了python的sys模块,大家参考使用吧 复制代码 代码如下: import os,sysbase = 'C:/'i = 1for j in range(100):    file_name = base+str(i)    os.mkdir(file_name)    i=i+1

  • Python3.5 创建文件的简单实例

    实例如下所示: #coding=utf-8 ''' Created on 2012-5-29 @author: xiaochou ''' import os import time def nsfile(s): '''The number of new expected documents''' #判断文件夹是否存在,如果不存在则创建 b = os.path.exists("E:\\testFile\\") if b: print("File Exist!") el

  • python脚本实现统计日志文件中的ip访问次数代码分享

    适用的日志格式: 106.45.185.214 - - [06/Aug/2014:07:38:59 +0800] "GET / HTTP/1.0" 200 10 "-" "-" 171.104.119.22 - - [06/Aug/2014:08:55:01 +0800] "GET / HTTP/1.0" 200 10 "-" "-" 27.31.238.242 - - [06/Aug/

  • python 实时遍历日志文件

    open 遍历一个大日志文件 使用 readlines() 还是 readline() ? 总体上 readlines() 不慢于python 一次次调用 readline(),因为前者的循环在C语言层面,而使用readline() 的循环是在Python语言层面. 但是 readlines() 会一次性把全部数据读到内存中,内存占用率会过高,readline() 每次只读一行,对于读取 大文件, 需要做出取舍. 如果不需要使用 seek() 定位偏移, for line in open('fi

  • python爬虫自动创建文件夹的功能

    该爬虫应用了创建文件夹的功能: #file setting folder_path = "D:/spider_things/2016.4.6/" + file_name +"/" if not os.path.exists(folder_path): os.makedirs(folder_path) 上面代码块的意思是: "os.path.exists(folder_path)"用来判断folder_path这个路径是否存在,如果不存在,就执行&

  • Python创建文件和追加文件内容实例

    一.用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行: 复制代码 代码如下: #python >>>f=open('f.txt','w')    # r只读,w可写,a追加 >>>for i in range(0,10):f.write(str(i)+'\n') .  .  . >>> f.close() 二.文件内容追加,从0到9的10个随机整数: 复制代码 代码如下: #python >>>import ran

随机推荐