2小时玩转python多线程编程
b站视频链接
from threading import Thread
import threading
import os
#copy函数
def copyFile(source_dir,dest_dir,filename):
#拼接后的路径
source_dir = os.path.join(source_dir , filename)
dest_dir = os.path.join(dest_dir , filename)
with open(source_dir,'rb') as sFile:
with open(dest_dir,'wb') as dFile:
while True:
data = sFile.read(1024)
if len(data) > 0:
dFile.write(data)
else:
break
# print("线程%d copy finally!" %os.getpid())
# print(threading.current_thread().name," ",threading.current_thread().ident)
def main():
# 定义资源目录和目标目录
source_dir = r'D:\垃圾东西\录像内容' #改成自己的
dest_dir = r'D:\垃圾东西\尚硅谷20天java核心技术教程\test' #改成自己的
# 创建目标目录
try:
os.mkdir(dest_dir)
except:
print("路径已存在")
# source目录下的所有目录、文件列表
source_list = os.listdir(source_dir)
for filename in source_list:
# 为每个文件创建一个线程
Thread(target=copyFile, args=(source_dir, dest_dir, filename)).start()
if __name__ == '__main__':
main()