python使用7z解压软件备份文件脚本分享

yipeiwu_com5年前Python基础

要求安装:

1.Python
2.7z解压软件

backup_2.py

复制代码 代码如下:

# Filename: backup_2.py

'''Backup files.
    Version: V2, based on Python 3.3
    Usage: backup.py -s:"dir1|dir2|..." -t:"target_dir" [-c:"comment"]
        -s: The source directories.
        -t: The target directory.
        -c: Optional, any comment.
    Examples:
        backup.py -s:"c:\\src\\F1|c:\\src\\F2|c:\\src\\F 3" -t:"c:\\backup"
        backup.py -s:"c:\\src\\F 3" -t:"c:\\backup" -c:"For sample"'''

import os
import sys
import time

# Read sys.argv
print(sys.argv)
if len(sys.argv) < 2:
    print(__doc__)
    sys.exit()

source=[]
target_dir=''
comment=''
for arg in sys.argv:
    if arg.startswith('-s:'):
        source=arg[3:].split('|')
        print(source)
    elif arg.startswith('-t:'):
        target_dir=arg[3:]+os.sep
        print(target_dir)
    elif arg.startswith('-c:'):
        comment=arg[3:]
        print(comment)

for i in range(0, len(source)):
    source[i] = "\"" + source[i] + "\""
    print(source[i])

# Make the file name with the time and comment
today=target_dir+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')

if len(comment)==0: # check if a comment was entered
    target=today+os.sep+now+'.7z'
else:
    target=today+os.sep+now+'_'+\
            comment.replace(' ','_')+'.7z'

# Create the subdirectory by day
if not os.path.exists(today):
    os.mkdir(today) # make directory
    print('Successfully created directory',today)

# zip command
zip_command="7z a %s %s" %(target,' '.join(source))
print(zip_command)

# Run the backup
if os.system(zip_command)==0:
    print('Successful backup to',target)
else:
    print('Backup FAILED')

相关文章

python 批量修改 labelImg 生成的xml文件的方法

概述 自己在用labelImg打好标签后,想只用其中几类训练,不想训练全部类别,又不想重新打标生成.xml文件,因此想到这个办法:直接在.xml文件中删除原有的不需要的标签类及其属性。...

Django实现跨域的2种方法

jsonp 方式一:指定返回方法 # 后端 def view(request): callback = request.GET.get('callback') return...

在pycharm中设置显示行数的方法

下面是具体的步骤,试用于pycharm2016(亲测) 1.  File-->settings-->editor-->general-->appeara...

python 利用浏览器 Cookie 模拟登录的用户访问知乎的方法

python 利用浏览器 Cookie 模拟登录的用户访问知乎的方法

首先在火狐浏览器上登录知乎,然后使用火狐浏览器插件 Httpfox 获取 GET 请求的Cookie,这里注意使用状态值为 200(获取成功)的某次GET. 将 Cookies 复制出...

python自动发邮件总结及实例说明【推荐】

python自动发邮件总结及实例说明【推荐】

python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构...