欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

windows系统中python使用rar命令压缩多个文件夹示例

程序员文章站 2023-11-03 21:30:28
复制代码 代码如下:#!/usr/bin/env python# filename: backup_ver1.py import osimport time # 1....

复制代码 代码如下:

#!/usr/bin/env python
# filename: backup_ver1.py

import os
import time

# 1. the files and directories to be backed up are specified in a list.
#source=['/home/swaroop/byte','/home/swaroop/bin']
source=['d:\\filecopier\\*.*','d:\\jeecms_doc\\*.*']
# if you are using windows, use source=[r'c:\documents',r'd:\work'] or something like that

# 2. the backup must be stored in a main backup directory
#target_dir='/mnt/e/backup/' #remember to change this to what you will be using
target_dir='e:\\temp\\' #remember to change this to what you will be using

# 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')+'.zip'

# 5. we use the zip command (in unix/linux) to put the files in a zip archive
#zip_command="zip -qr '%s' %s" %(target,' '.join(source))
zip_command="rar a " + target + ' '.join(source)
# run the backup
if os.system(zip_command)==0:
 print 'successful backup to',target
else:
 print 'backup failed'