文件备份

'''

实现输入当前文件夹 目标文件夹

把当前文件夹以及其子文件夹里的TXT结尾的文件都备份到目标文件夹

'''


import os

import shutil
import 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(' ')


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值