您的位置:首页 > 编程语言 > Python开发

Python备份文件脚本

2013-08-07 10:53 316 查看
放假闲来无聊,开始学习python.

开始写到一个关于文件备份的脚本,但是给的例子只有Linux和Unix系统下的示例代码,我的Linux刚刚被删,只能在window下。

示例代码中就在一个地方出现问题就是zip_command那里,不同的系统,在命令行下压

缩文件的命令也不一样。

我这里用的是Rar。

一般window都会自带一个WinRar程序,但是我们平常都是利用它自带的图形界面来操作,其实它的压缩操作可以在命令行进行。

步骤如下:

1.复制 C:\Program Files\WinRAR/WinRAR.exe 到 C:\window\system32/ 下

2.在命令行输入 rar a C:\backup\test C:\Documents (前面C:\backup\test表示要备份好的文件要存放的位置,最后一个\后面表示文件名称.后面C:\Documents表示要备份的文件)

命令解决了,python代码就出来了

#FileName:backup_ver1.py

import os
import time

# 1. The files and directories to be backed up are specified in a list.
source = r'C:\Documents'
# if you are uing Linux/Unix, use source = ['/home/swaroop/byte','home/swaroop/bin'] or somthing like that

# 2. The backup must be stored in a main backup directory
target_dir = r'C:\backup\\test'

# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time
target = target_dir + time.strftime('%Y%m%d%H%M%S')

# 5. We use the zip command in Linux or Unix
#zip_command = "rar a %s %s" % (target, source)
#This command for window
zip_command = 'rar a '+ target + ' '+ source #Pay attention to the ' '
print zip_command
# Run the backup
if os.system(zip_command) == 0:
print 'Successful backup to',target
else:
print 'Backup FAILED'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: