实验室的一段Python脚本 “备份文件操作”

83 篇文章 0 订阅

#将原有文件备份,并清空原文件内容

#!/usr/bin/env/ python
#coding=utf-8
import shutil
import os 
import sys
from time import strftime, localtime 
#from datetime import timedelta, date 
import calendar 

#文本备份接口
def BackupData(src_path, src_fileName, dst_path):
	#获取当前格式化时间
	year = strftime("%Y",localtime()) 
	mon  = strftime("%m",localtime()) 
	day  = strftime("%d",localtime()) 
	hour = strftime("%H",localtime()) 
	min  = strftime("%M",localtime()) 
	sec  = strftime("%S",localtime()) 

	#处理源路径和目标路径
	#src_path = os.getcwd()
	#src_path = sys.path[0]
	src = src_path + '/' + src_fileName
	dst = dst_path + '/' + year + '_' + mon + '_'+ day + '_'+ hour + min + sec + '.dat'

	#开始备份
	shutil.copyfile(src, dst)

	#清空原有数据
	#os.remove(src)
	fileTmpHandler = file(src, 'w')
	fileTmpHandler.close

# 测试部分
if __name__ == '__main__':
	srcPath = sys.path[0] + '/data'
	BackupData(srcPath, 'hottopic.dat', srcPath)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值