Python黑科技 实现U盘自动读取复制,并实现打包

import os,time,shutil,re
 
 
 
#识别文件的格式
regex_filename = re.compile(r'(.*zip$)|(.rar$)|(.*docx$)|(.*ppt$)|(.*xls$)|(.*py$)')
 
def usb_monitor(sleep_time,usb_path,copy_folder):
 
 
    if not os.path.exists(copy_folder):
        os.mkdir(copy_folder)
 
    a = 0
 
    while True:
 
        time.sleep(sleep_time)
 
 
        start_time = time.time()
        file_num = 0
 
        try:
            u = os.listdir(usb_path)
        except FileNotFoundError:
            print('usb is not found')
            a=0
            continue
 
        if a>0:
            print('usb has already copied')
            continue
 
        copy_folder = os.path.join(copy_folder, time.strftime('%Y-%m-%d %H-%M', time.localtime()))
        os.mkdir(copy_folder)
        for root, dirs, files in os.walk(usb_path):
            if a == 0:
                print('start copy usb is {}'.format(dirs[0]))
                usb_name = dirs[0]
 
            a += 1
 
            if len(files)==0:
                continue
 
            for name in files:
 
                file = os.path.join(root,name)
                if regex_filename.match(file) and os.path.getsize(file) < 1024*1024*2:
                    file_num+=1
 
 
                    print(file)
                    # file_create_time = time.ctime(os.path.getctime(file))
                    file_create_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.path.getctime(file)))
 
                    shutil.copy2(file,copy_folder)
                else:
                    continue
        if file_num==0:
            print('usb is not found file')
            continue
        print('共下载{}份文件---用时:{}s'.format(file_num,time.time()-start_time))
 
 
 
 
usb_monitor(1,r'F:/',r'save_path')
 
 

打包文件:

pyinstall -F -i   image.ico   demo.py

ps:image.ico 是exe程序的图标可以去https://www.easyicon.net/34713-heart_icon.html 下载自己想要的图标

打包之后会在当前目录生成一个dist 文件,里面就是生成的exe文件了,点击!!!!神奇的事情发生了!!!

Python黑科技 实现U盘自动读取复制,并实现打包_第1张图片

你可能感兴趣的:(python)