自动备份某一文件夹下的Json文件
查找文件夹下所有文件判断是文件还是文件夹
def get_json ( path) :
for file in os. listdir( path) :
fs = os. path. join( path, file )
if os. path. isfile( fs) :
if not fs. endswith( ".json" ) :
os. remove( fs)
elif os. path. isdir( fs) :
get_json( fs)
判断是否备份过
def get_drivers ( drivers_path, path) :
if not os. path. exists( path) :
shutil. copytree( drivers_path, path)
else :
shutil. rmtree( path)
shutil. copytree( drivers_path, path)
完整代码
import os
import shutil
import sys
def get_drivers ( drivers_path, path) :
if not os. path. exists( path) :
shutil. copytree( drivers_path, path)
else :
shutil. rmtree( path)
shutil. copytree( drivers_path, path)
recurrence( path)
def recurrence ( path) :
for file in os. listdir( path) :
fs = os. path. join( path, file )
if os. path. isfile( fs) :
if not fs. endswith( ".json" ) :
os. remove( fs)
elif os. path. isdir( fs) :
recurrence( fs)
if __name__ == "__main__" :
drivers = sys. argv[ 1 ]
drivers_bak = sys. argv[ 2 ]
get_drivers( drivers, drivers_bak)
写入bat文件双击调用
@echo off
start python3.7.9\python backups.py .\drivers .\drivers_bak
exit