I have been thinking about Python, but I have never been idle. This is not the case. On Saturday, boxing is also there. So I started from the beginning, reading the python manual, and reading a concise manual for two hours, then write down
This script implements incremental file backup ...... Looking back, it is not complicated. Except for a recursion, there is only the limit method, and the latter makes us spend a lot of time. Some operations on the server side of Python are very convenient. I feel that this is a very flattering language,CodeAs follows:
# ! /Usr/bin/Python
# -*-Coding: UTF-8 -*-
# Filename: auto_bak.py
# Author: ZZ
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 the file path is a directory
If OS. Path. isdir (file_path ):
# If the folder in the backup directory does not exist, create
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 the file already exists, compare the file modification time
If OS. Path. isfile (file_path_bak ):
Stat_bak = OS. Stat (file_path_bak)
Stat_source = OS. Stat (file_path)
# Determine the file modification time
If Stat_source.st_mtime <= Stat_bak.st_mtime:
Continue
Cp_com = ''' CP '% s' ''' \
% (File_path, file_path_bak)
If OS. System (cp_com) = 0:
Print Cp_com
Else :
Print ' Create folder failure! '
OS. _ exit (0)
# File directory to be backed up
Path = ' /Home/ZYF/appspot/auto_bak/ '
# Backup file directory
Path_bak = ' /Home/ZYF/appspot/auto_bak/bak '
# Start backup
Bak_file (path, path_bak)