Popen通信方法自动打开文件,停止程序执行,直到我手动关闭文件

2024-10-02 02:27:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python子流程模块有一个问题

import os, subprocess

BLEU_SCRIPT_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'multi-bleu.perl')
command = BLEU_SCRIPT_PATH + ' %s < %s'
ref = "ref.en-fr.test.txt"
hyp = "hyp100.en-fr.test.txt"

p = subprocess.Popen(command % (ref, hyp), stdout=subprocess.PIPE, shell=True)
result = p.communicate()[0].decode("utf-8")
# ...
# ...

multi-bleu.perl文件进行计算并返回实数或错误(如果有);但这不是我关心的。
最后一行代码使用我的默认文本编辑器自动打开multi-bleu.perl文件,停止程序执行,直到我手动关闭该文件。
如何禁用此行为


Tags: 文件pathtestrefosscriptfrmulti
2条回答

我不认为subprocess.Popen解释文件中的任何shebang。您需要在要执行的命令中指定可执行文件。此外,Popen需要一个列表作为第一个参数,因此您需要将字符串格式“提升”到命令列表中

command = [
    '/path/to/perl',
    BLEU_SCRIPT_PATH + ' %s < %s' % (ref, hyp)
]
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)

您可能还想看看^{},这将使代码更容易一些

谢谢你的帮助,非常有帮助。我在linux平台上编写代码(显然有一个默认的perl解释器),当我回到windows时,我并没有注意到这种方式。一旦安装了perl解释器并将其添加到环境中(您建议的内容):

command = BLEU_SCRIPT_PATH + ' %s < %s'
if os.name == 'nt' : # windows os
    command = "perl " + command

相关问题 更多 >

    热门问题