Python 創建文件備份

一個簡單的例子,A byte of Python的例子,利用系統自帶的命令創建某些文件的備份,放入指定的目錄中。

簡單功能已具有,先記下,今後可以和網盤接口連接。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import time
 
source = ['/home/dat/python/test1','/home/dat/python/test2']
 
target_dir = '/home/dat/python/'
 
today = target_dir+time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
 
 
comment = raw_input('Enter a comment:')
if len(comment) == 0:
    target = today+os.sep+now
else:
    target = today+os.sep+now+'_'+comment.replace(' ','_')+'.zip'
 
if not os.path.exists(today):
    os.mkdir(today)
    print 'successfully created directory',today
 
tar_command = 'tar -cvzf %s %s -X /home/dat/python/excludes.txt' % (target, ' '.join(source))
 
if os.system(tar_command)==0:
    print 'Success back to',target
else:
    print 'Back up Failed'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章