python - 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. 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.
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. 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:
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')
推荐阅读
- javascript - 刷新 div 后日期选择器不起作用
- neo4j - 不跨越某些节点的密码路径
- c++ - Cmake Visual Studio - 在输出文件夹位置创建的文件
- java - 从 CDI 代理获取真实对象
- android - 在中间有间隙的线性布局中创建两列
- here-olp - 通过 HERE OLP 数据可视化库在自定义数据中使用 64 位整数
- python - Python。收集数据直到json文件结束
- javascript - laterJS 中的多个 setTimeouts 不按计划进行
- c# - 如何从 MVC c# 中的一个 viewModel 保存来自两个模型(关系一对多)的数据?
- elixir - GenServer 可以在 Elixir 中拥有自己的结构吗?