python: 通过脚本实现重要文件的备份
2015-12-15 19:42
846 查看
python: 通过脚本实现重要文件的备份
标签:python 脚本 备份by 小威威
入门python两周了,我觉得自己应该用python来写一点小东西来玩一玩,所以我就写了一个实现重要文件备份的脚本,现在来展示我的代码。
#!/usr/bin/python3 # Filename: backup_ver3.py # Function: Help us backup some important document more conveniently # declare The modules os and time import os import time # source is the document that we want to backup # today_dir is the path of the new folder # today is the folder that we want to establish # now is the name of the zip source = ['/Users/apple/Desktop/python'] today_dir = '/Users/apple/Desktop/python/' today = today_dir + time.strftime('%Y%m%d') now = time.strftime('%H%M%S') # Judge whether the folder of time is exist, if not, establish it if not os.path.exists(today): os.mkdir(today) print ('Successful mikdir!') # The comment we want to add to the backup string = input('Please enter what you want to note:') # judge whether the users have entered the comment,if not, carry out the original way, else add the comment to the name of backup # os.sep represents the seperation of address,because in different system, the seperation is different if len(string) == 0: target = today + os.sep + now + '.zip' else : target = today + os.sep + now + '_' + \ string.replace(' ', '_') + '.zip' # The command is the string that excute in terminal command = 'zip -qr %s %s' % (target, ''.join(source)) # if the backup is success, the system will return 0 if os.system(command) == 0: print ('Sucessfully backup to', target) else: print ('Backup failed')
输出结果:
appledeMacBook-Pro-2:~ apple$ cd Desktop appledeMacBook-Pro-2:Desktop apple$ cd python appledeMacBook-Pro-2:python apple$ python3 backup_ver3.py Please enter what you want to note:important Sucessfully backup to /Users/apple/Desktop/python/20151215/195936_important.zip
有了这个脚本,当我们要备份文件时,只需要执行脚本就能够实现。这个脚本的优点是:能够较快实现文件的压缩,而且每一天都有对应日期文件夹,文件以时间命名,同时还可以添加自己的注释。换句话说,这个脚本使我们的备份很有条理,倘若我们自己通过软件操作,时间一定大大超过脚本执行时间,所以,这个脚本还是有一定的价值的。它在一定程度上方便了我对重要文件的备份操作。
以上内容皆为本人观点,欢迎大家提出批评和指导,我们一起探讨。
相关文章推荐
- python基础教程共60课-第41课用文件保存游戏3
- 在工作过程中,对RabbitMQ的一些体会
- Python基础教程 第2章: 列表和元组 学习笔记
- 关于Python升级版本出现的问题
- Python学习_2015年12月15日
- python R 热度图聚类demo
- python 使用 BeautifulSoup 解析html
- day8 python学习随笔(上)
- python 模拟 java hashcode
- leetcode之Balanced Binary Tree
- 机器学习实战python版Logistic回归
- 零基础入门学习Python(11):内嵌函数、闭包和变量作用域
- 基本采样算法及Python实现
- AES ECB加密实现(java/php/python)
- python中的quopri模块
- Python单例模式的4种实现方法
- Python基础教程 第1章: 基础知识 学习笔记
- Python之路【番外篇】回顾&类的静态字段
- python学习---一些特殊函数
- Python3.2官方文件翻译--课堂笔记和异常是阶级