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代码就出来了
开始写到一个关于文件备份的脚本,但是给的例子只有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'
相关文章推荐
- python备份文件以及mysql数据库的脚本代码
- python 备份文件的小脚本
- python备份文件的脚本
- Python小脚本 001 备份文件
- python 备份文件脚本
- python备份文件的脚本
- 实验室的一段Python脚本,根据用户指定路径来备份文件
- 备份文件的python脚本(转)
- 初学python —— 备份文件脚本
- 破解栅栏密码python脚本
- Python 脚本 批量删除目录下文件的特定内容
- Python脚本简介
- pdb调试python脚本
- 【详解】Python爬虫脚本M9优化
- nodejs调用脚本(python/shell)和系统命令
- Shell脚本自动删除指定大小以上的备份文件
- [Python]HTML转换为TXT的脚本
- 【脚本语言系列】关于Python性能分析工具timeit, 你需要知道的事
- 在arcgis使用python脚本进行字段计算时对中文的处理方案
- Python脚本sendmail