Python 备份程序代码实现

yipeiwu_com5年前Python基础

Python的一个备份程序

这是一个备份脚本。路径请自行更换。

这是一个备份脚本,按照当前日期分目录,以时间作为文件名,并且可以在文件名加入备注信息.

以zip方式作为压缩方式, 有特殊需求可以更改.

实例代码:


#! /usr/bin/python
#coding=utf-8
 
#这是一个备份脚本,按照当前日期分目录,以时间作为文件名,并且可以在文件名加入备注信息.
#以zip方式作为压缩方式, 有特殊需求可以更改.
import os
import time
 
source = ['/home/leeicoding/workspace/j2ee','/home/leeicoding/workspace/python']
 
target_dir = '/home/leeicoding/bak'
#获取系统时间
today = target_dir + time.strftime('%Y%m%d')
now  = time.strftime('%H%M%S')
# 输入备注
comment = raw_input('请输入备注:')
if len(comment) == 0:
  print('无备注')
  target = today + os.sep + now + '.zip'
else:
  target = today + os.sep + now + comment.replace(' ','_') + '.zip'
 
if not os.path.exists(today):
  os.mkdir(today)
  print('创建目录'+today+'成功')
 
 
# 备份命令
# q 静默方式 r递归目录
zip_command = 'zip -qr "%s" %s' % (target, ' '.join(source))
 
if os.system(zip_command) == 0:
  print('备份成功,存放在: '+target)

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

Python中顺序表的实现简单代码分享

Python中顺序表的实现简单代码分享

顺序表python版的实现(部分功能未实现) 结果展示: 代码示例: #!/usr/bin/env python # -*- coding:utf-8 -*- class Seq...

python多任务之协程的使用详解

1|0使用yield完成多任务 import time def test1(): while True: print("--1--") time.sleep(0.5)...

python 将字符串转换成字典dict

复制代码 代码如下:JSON到字典转化:dictinfo = simplejson.loads(json_str) 输出dict类型 字典到JSON转化:jsoninfo = simpl...

Python编程在flask中模拟进行Restful的CRUD操作

Python编程在flask中模拟进行Restful的CRUD操作

这篇文章中我们将通过对HelloWorld的message进行操作,介绍一下如何使用flask进行Restful的CRUD。 概要信息 事前准备:flask liumiaocn:f...

python写的ARP攻击代码实例

注:使用这个脚本需要安装scapy 包最好在linux平台下使用,因为scapy包在windows上安装老是会有各种问题 复制代码 代码如下:#coding:utf-8#example...