'''
实现输入当前文件夹 目标文件夹
把当前文件夹以及其子文件夹里的TXT结尾的文件都备份到目标文件夹
'''
import os
import shutilimport re
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.debug('Start of program')
logging.disable(logging.ERROR)
GenPath = r'E:\ '
OutPath = r'E:\file_bak2'
FileAim = re.compile('.txt$')
print('please input 1 选择 汉语')
print('please input 2 chose English')
try:
id = int(input())
except Exception as ImportError:
print('input error')
if id == 1:
print('请输入要遍历的目录:')
GenPath = input()
print('请输入要备份的路径的文件夹名:')
OutPath = input()
print('GenPath:'+ GenPath + ' OutPath:' + OutPath)
elif id == 2:
print('Please enter the directory you want to traverse:')
GenPath = input()
print('Please enter a folder name for the path you want to backup:')
OutPath = input()
print('GenPath:'+ GenPath + ' OutPath:' + OutPath)
else:
print('input ERROR')
1
try:
os.makedirs(OutPath)
except Exception as FileExistsError:
logging.debug(OutPath + ' exist')
for folderName , subfolders, filenames in os.walk(GenPath):
for filename in filenames:
mo = FileAim.search(filename)
if mo == None:
newFilename = folderName+ '\\' + filename
logging.debug(newFilename)
else:
newFilename = folderName + '\\' + filename
logging.debug(newFilename)
try :
shutil.copy(newFilename,OutPath)
except Exception as err:
logging.error(newFilename)
print(' ')