- 前言
一、查看电脑核心数
二、文件目录结构
三、Python代码
- 总结
由于工作需要,每次讲座过后都会送出英特尔FPGA中国创新中心U盘,U盘里面会拷贝一些FPGA精品课程视频,每个U盘都需要进行ctrl+c,ctrl+v的重复工作。 高速发展的社会需要的是Python的自动化办公。 这节课就使用Python开启多进程进行视频的拷贝,解放生产力。
一、查看电脑核心数
win + r输入cmd
输入 wmic
输入 cpu get *
核心数为4
二、文件目录结构
三、Python代码
from multiprocessing import Process
import os
import string
import shutil
video_dir = os.getcwd()+"/video" # 获取要拷贝的文件路径
disks = []
for c in string.ascii_uppercase: # 获取盘符
disk = c + ':'
if os.path.isdir(disk) and disk not in ["C:", "D:", "E:", "F:"]: # 去除自己电脑固定的盘,剩下的就是u盘
disks.append(disk)
# 拷贝函数
def copy_process(disk):
print("进程:"+str(os.getpid())) # 获取进程id
if len(os.listdir(disk)) == 1: # u盘里面有一个隐藏文件
for file in os.listdir(video_dir): # 将video里面的文件全部拷贝到u盘里面
cpath = os.path.join(video_dir, file)
shutil.copyfile(cpath, os.path.join(disk, file)) # 拷贝
print(disk, "拷贝完成!")
else:
print(disk, "已经拷贝!")
if __name__ == "__main__":
ps = []
for disk in disks:
p = Process(target=copy_process, args=(disk,)) # 申请进程,有几个u盘就申请几个,进程数最好和电脑核心数一致
ps.append(p)
for p in ps:
p.start() # 开启进程
总结
天下功夫,唯快不破,快使用多进程尽情地压榨你的电脑性能吧!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)