简体   繁体   English

Subprocess.Popen 没有完成被调用的脚本

[英]Subprocess.Popen doesnt finish the called script

My goal is to call a different python script by using subprocess.Popen and save the results inside of a txt file.我的目标是通过使用 subprocess.Popen 调用不同的 python 脚本并将结果保存在 txt 文件中。 I used我用了

process = subprocess.Popen("start cmd /c abq2020 python " + directory + os.sep + "data_mean.py", shell=True)
process.wait()

file = np.loadtxt(directory + os.sep + 'Values.txt')

in my main program and在我的主程序和

f = open(varpath + os.sep + 'Values.txt','w+')

for i in Output:
    for j in i:
        f.write(str(j) + '     ')
    f.write('\n')
    
f.close()

in the subprogram.在子程序中。

Now my problem is that the main program won't wait until the subprogram created the txt file and thus produces an error message.现在我的问题是主程序不会等到子程序创建 txt 文件并因此产生错误消息。

I used this method because I dont know an other way to pass variables between the main and the sub process.我使用这种方法是因为我不知道在主进程和子进程之间传递变量的另一种方法。 I appreciate any ideas and advices.我感谢任何想法和建议。

Thanks in advance.提前致谢。

I don't know what apq2020 is and why it is preceding the python command, but I assume you know what you are doing.我不知道apq2020是什么以及为什么它在 python 命令之前,但我假设您知道自己在做什么。 You do not need to nor should you be starting up a command prompt.您不需要也不应该启动命令提示符。 This is all you should need in addition to providing lots of missing import statements:除了提供许多缺少的import语句之外,这就是您所需要的:

process = subprocess.Popen("abq2020 python " + directory + os.sep + "data_mean.py", shell=True)
process.communicate() # process.wait() will work in this case, too

file = np.loadtxt(directory + os.sep + 'Values.txt')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM