標籤:python
# !/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import sys
def get_dir(path):
print(path, '\n')
return os.listdir(path)
def bak_file(path, path_bak):
list = os.listdir(path)
for l in list:
file_path = os.path.join(path, l)
file_path_bak = os.path.join(path_bak, l)
print
file_path
# 如果檔案路徑為目錄
if os.path.isdir(file_path):
# 如果在備份目錄中檔案夾不存在則建立
if not os.path.isdir(file_path_bak):
create_com = '''mkdir -p '%s' ''' \
% (file_path_bak)
if os.system(create_com) == 0:
print(create_com)
else:
print('create folder failure!',os._exit(0))
bak_file(file_path, file_path_bak)
else:
# 如果檔案已經存在,則比較檔案修改時間
if os.path.isfile(file_path_bak):
stat_bak = os.stat(file_path_bak)
stat_source = os.stat(file_path)
# 判斷檔案修改時間
if stat_source.st_mtime <= stat_bak.st_mtime:
continue
cp_com = '''cp '%s' '%s' ''' \
% (file_path, file_path_bak)
if os.system(cp_com) == 0:
print(cp_com)
else:
print('create folder failure!',os._exit(0))
# 要備份的檔案目錄
path = str(raw_input('請輸入需要備份目錄:'))
# 備份檔案目錄
path_bak = str(raw_input('請輸入儲存目錄'))
# 開始備份
bak_file(path, path_bak)
python實現增量備份目錄