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自定义函数定义,参数,调用代码解析

函数能提高应用的模块性,和代码的重复利用率。Python提供了许多内建函数,比如print()等。也可以创建用户自定义函数。 函数定义 函数定义的简单规则: 函数代码块以def关键词开头...

Python连接SQLServer2000的方法详解

本文实例讲述了Python连接SQLServer2000的方法。分享给大家供大家参考,具体如下: http://pymssql.sourceforge.net/  介绍PYTH...

Eclipse中Python开发环境搭建简单教程

Eclipse中Python开发环境搭建简单教程

一、背景介绍   Eclipse是一款基于Java的可扩展开发平台。其官方下载中包括J2EE方向版本、Java方向版本、C/C++方向版本、移动应用方向版本等诸多版本。除此之外,Ecli...

在python中计算ssim的方法(与Matlab结果一致)

如下代码可以计算输入的两张图像的结构相似度(SSIM),结果与matlab计算结果一致 // An highlighted block import cv2 import numpy...

Python实现简单文本字符串处理的方法

本文实例讲述了Python实现简单文本字符串处理的方法。分享给大家供大家参考,具体如下: 对于一个文本字符串,可以使用Python的string.split()方法将其切割。下面看看实际...