我在 python 中创建了一个小脚本,我想在其中使用多处理同时执行两个函数。第一个函数将执行目录递归搜索,第二个函数将向用户显示一些问题。尽管创建了 .txt 文件,但问题并未出现。我见过这个问题:Python command line input in a process但作为初学者,我不明白问题是什么以及如何解决它。这是我的脚本:
import os
import thread
import time
from multiprocessing import Process
def writeFiles():
#open a file for writing files in it
f = open("testFile.txt","w")
#do the walk
for root ,dirs,files in os.walk('C:\\Users'):
for dir in dirs:
if dir.startswith('Test'):
for root ,dirs,files in os.walk('C:\\Users\\' + dir +'\Desktop'):
for file in files:
if file.endswith('.txt'):
#include the full path
f.write( os.path.join(root, file + "\n") )
#close the file
f.close()
def ask():
a = raw_input('Your name? ')
if a == 'Tester':
print 'Hello'
else:
print 'Bye'
if __name__ == '__main__':
# create processes
p1 = Process( target = writeFiles)
p2 = Process( target = ask)
p1.start()
p2.start()