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

yipeiwu_com6年前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利用OpenCV2实现人脸检测

python利用OpenCV2实现人脸检测

最近,带领我的学生进行一个URTP项目设计,需要进行人脸识别。由于现在的OpenCV已经到了2.X版本,因此就不想用原来的1.X版本的代码,而网上存在的代码都是1.X版本的代码,尝试自己...

Python 多个图同时在不同窗口显示的实现方法

Python的matplotlib包可以轻松的将数据可视化,博主最近遇到了一个问题,博主想同时在两个窗口展示两张图,但是代码运行结果总是显示一张图,把当前的图删掉之后才能显示另一张图。网...

python定时检查某个进程是否已经关闭的方法

本文实例讲述了python定时检查某个进程是否已经关闭的方法。分享给大家供大家参考。具体如下: import threading import time import os impo...

Python 3.6 中使用pdfminer解析pdf文件的实现

Python 3.6 中使用pdfminer解析pdf文件的实现

所使用python环境为最新的3.6版本 一、安装pdfminer模块 安装anaconda后,直接可以通过pip安装 pip install pdfminer3k  ...

Python操作列表常用方法实例小结【创建、遍历、统计、切片等】

Python操作列表常用方法实例小结【创建、遍历、统计、切片等】

本文实例讲述了Python操作列表常用方法。分享给大家供大家参考,具体如下: 使用for循环,遍历整个列表 依次从列表中取出元素,存放到names变量中,并拼接打印 names =...