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简单实现子网掩码转换的方法

本文实例讲述了Python简单实现子网掩码转换的方法。分享给大家供大家参考,具体如下: 这里实现将子网掩码长度转换为具体的子网掩码地址: def exchange_maskint(m...

Python Pandas 箱线图的实现

Python Pandas 箱线图的实现

各国家用户消费分布 import numpy as np import pandas as pd import matplotlib.pyplot as plt data...

python中实现数组和列表读取一列的方法

在python中,普通的列表list和numpy中的数组array是不一样的,最大的不同是:一个列表中可以存放不同类型的数据,包括int、float和str,甚至布尔型;而一个数组中存放...

面向初学者的Python编辑器Mu

面向初学者的Python编辑器Mu

Meet Mu,一个开放源码编辑器,使学生们更容易学习编写Python代码。 Mu一个开源编辑器,是满足学生可以轻松学习编写Python代码的工具。作为初学程序员的Python编辑器,旨...

python3使用scrapy生成csv文件代码示例

python3使用scrapy生成csv文件代码示例

去腾讯招聘网的信息,这个小项目有人做过,本着一个新手学习的目的,所以自己也来做着玩玩,大家可以参考一下。 这里使用的是调用cmdline命令来生成csv文件,而不是importcsv模块...