Sep-07-2018, 02:39 PM
I wrote this code to transfer all the video files from the download folder to the usb.
import threading from threading import Thread import os, shutil, time #patch from https://stackoverflow.com/questions/21799210/python-copy-larger-file-too-slow def _copyfileobj_patched(fsrc, fdst, length=16*1024*1024): """Patches shutil method to hugely improve copy speed""" while 1: buf = fsrc.read(length) if not buf: break fdst.write(buf) shutil.copyfileobj = _copyfileobj_patched #patch from https://stackoverflow.com/questions/21799210/python-copy-larger-file-too-slow download = "C:\\Users\\AC Milan\\Downloads\\" USB = "G:\\" video_format = (".3gp",".asf",".avi",".divx",".flv",".swf",".mp4",".mpeg",".mpg",".ogm",".wmv",".mov",".mkv",".nbr",".rm",".vob",".sfd",".webm",".xvid") current_file = "" current_file_size = 0 def move_file(): global current_file global current_file_size os.chdir(download) for dirs, s_dirs, files in os.walk(download): for file in files: for v_format in video_format: if v_format in file: os.chdir(str(dirs)) current_file = file current_file_size = os.stat(file).st_size shutil.copy(file,USB) current_file = "" print(f"►File [[ {file} ]] transfered ") print("All files transfered") def get_file_size(folder): os.chdir(folder) file_size = 0 for dirs, s_dirs, files in os.walk(folder): for file in files: for v_format in video_format: if v_format in file: os.chdir(str(dirs)) file_size += os.stat(file).st_size return file_size def perc(): file_size = get_file_size(download) moved_size = get_file_size(USB) return int(moved_size/file_size*100) def print_perc(): while True: perce = perc() current_transfered_size = os.stat(f"{str(USB)}{current_file}").st_size current_perc = int(current_transfered_size/current_file_size*100) print(f"|{'▒'*int(perce/2)}{' '*(50-int(perce/2))}| {perce}% {current_file[:20]} {current_perc}%",end="\r") time.sleep(0.1) if __name__ == '__main__': Thread(target = move_file).start() Thread(target = print_perc).start()