#使用以上三选一的方式,完成多进程程序同时写操作多个文件.比如,同时启动三个子进程,分别写三个不同文件
#A文件中存储随机大写字母
#B文件中存储随机小写字母
#C文件中存储随机数字
from multiprocessing import Process
import random
def fun():
count = 0
while True:
with open("./xiaoxie.txt","w+") as x:
for i in range(5):
xiao = chr(random.randrange(97, 122))
x.write(xiao)
break
count += 1
if count >= 5 :
break
def fun2():
count = 0
while True:
with open("./shuzi.txt","w+") as s:
for i in range(5):
shu = random.randint(1,100)
s.write(str(shu)+"\n")
break
count += 1
if count >= 5 :
break
def main():
pid = Process(target=fun)
pid.start()
pid2 = Process(target=fun2)
pid2.start()
while True:
with open("./daxie.txt","w+") as f :
for i in range(5):
buff = chr(random.randrange(65,91))
f.write(buff)
break
if __name__ == '__main__': #给程序一个入口
main() #入口函数调用
生成的文件:
大写文件:
小写文件:
数字文件: