在我的 previous question 之后这涉及到一个不同的问题,我想我会根据这个问题来构建。
注意:我已经尝试了所有版本的subprocess.Popen()
、communicate()
、wait()
,例如:
vowpal = subprocess.Popen('../../vowpal.sh',shell=True, stdout=subprocess.PIPE)
vowpal.communicate()
print vowpal.returncode
我有这样的文件结构:
├── src
│ ├── main
│ │ ├── costSensitiveClassifier.py
└── vowpal.sh
|
├── data
│ ├── output
│ │ ├── cost
| | |_______openCostClassifier.dat
| | |
在 costSensitiveClassifier.py
中,我实际上是在尝试运行一个名为 vowpal.sh
的脚本,该脚本对 openCostClassifer.dat
进行一些操作并将一些文件输出到与该文件相同的文件夹中。
costSensitiveClassifier.py 中的代码是:
import subprocess
print "Starting cost sensitive predictions using batch script\n"
subprocess.call("../../vowpal.sh")
print "Ending predictions"
vowpal.sh 中的代码是:
# !/bin/bash
vw --csoaa 24 data/output/cost/openCostClassifier.dat -f data/output/cost/csoaa.model
vw -t -i data/output/cost/csoaa.model data/output/cost/openCostClassifier.dat -p data/output/cost/csoaa.predict
问题始终是 bash 脚本的第二行需要先输出一个名为 csoaa.model
的东西(请参阅 bash 脚本的第一行),一旦完成,第二行应该运行,它使用该文件输出 csoaa.predict
。但是,情况并非如此,我在运行 python 文件时得到了与 bash 脚本相关的错误代码:
vw (./io_buf.h:123): can't open: data/output/cost/csoaa.modelerrno = No such file or directory
我找到了 this link但不确定它是否是我需要的(我是否应该将 sleep 调整为 bash 脚本完成并创建我需要处理的文件的预期时间?)。
最佳答案
这是 vowpal 中的一个问题。它试图创建一个错误文件,但它不能。原因之一可能是目录“data/output/cost/”不存在。
这就是导致我出现同样错误的原因。
关于python - 调用的子进程不按顺序执行 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38761929/