【问题标题】:Get Exceptions from popen in python从 python 中的 popen 获取异常
【发布时间】:2017-02-07 21:18:21
【问题描述】:

我在 python 中执行了这段代码:(test.py)

from subprocess import Popen
p = Popen("file.bat").wait()

这是file.bat:

@echo off
start c:\python27\python.exe C:\Test\p1.py %*
start c:\python27\python.exe C:\Test\p2.py %*
pause

这里是 p1.py:

This line is error
print "Hello word"

p2.py 没意思

我想通过运行 test.py 来了解 p1.py 中的异常(不仅仅是编译错误)? 我该怎么做?

谢谢!

【问题讨论】:

    标签: python subprocess popen


    【解决方案1】:

    我是这样工作的: test.py

    from subprocess import Popen
    p = Popen(["./file.sh"]).wait()
    

    确保在文件周围添加[],以及./。您还可以添加参数,如下所示: ["./file.sh", "someArg"] 请注意,我不在 Windows 上,但这在 Ubuntu 上修复了它。如果您仍有问题,请发表评论

    编辑: 我认为真正的解决方案是:Using subprocess to run Python script on Windows

    这样你可以从 python 运行 python 脚本,同时仍然使用 Popen

    【讨论】:

    • '.'未被识别为内部或外部命令,-我收到此错误
    • 为什么是 file.sh?我的文件是 .bat
    • 我在linux上,我的无法运行bat。只需将sh 替换为bat 并尝试一下。
    猜你喜欢
    • 2012-11-24
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 2011-05-17
    • 2019-04-01
    • 2021-01-31
    • 2010-12-09
    相关资源
    最近更新 更多