2

我正在编写一个测试脚本,它将简单地运行一个带有几个参数的 *.EXE 文件,然后将结果输出到一个文件中。我有一个 *.sh 测试脚本可以正确运行测试(但需要手动更新以进行更多测试)。此脚本中的行如下所示:

blah.exe arg1 arg2 arg3 > ../test/arg4/arg4.out

我已经编写了一个 python 脚本来根据一些简单的 python 模块自动生成参数(现在很粗糙):

import os
import subprocess

for test_dir in os.listdir():
    if not os.path.isdir(test_dir):
        continue
    # load a few variables from the ./test_dir/test_dir.py module
    test_module = getattr(__import__(test_dir, fromlist=[test_dir]), test_dir)

    # These arguments are relative paths, fix the paths
    arg1 = os.path.join(test_dir, test_module.ARG1)
    arg2 = os.path.join(test_dir, test_module.ARG2)
    arg3 = os.path.join(test_dir, test_module.ARG3)

    proc = subprocess.Popen(["../bin/blah.exe", arg1, arg2, arg3], stdout=subprocess.PIPE)

    stdout_txt = proc.stdout.read().decode("utf-8")

    with open(os.path.join(test_dir, test_dir + '.out'), 'w') as f:
        f.write(stdout_txt)

我打开了文件进行比较,当脚本运行时,我遇到了一个问题。第一个(shell)解决方案输出正确的行尾。第二个(python)解决方案输出以 CR CR LF 结尾的行。这在记事本中看起来是正确的,但在记事本++中,每隔一行似乎都是空白的:

这些行之间不应有空行

为什么 python 的输出给出了不正确的行尾?

如何更正行尾?(将 CR CR LF 更改为 CR LF 而无需编写另一个脚本)

4

1 回答 1

3

尝试使用universal_newlines=True参数Popen(参见http://docs.python.org/library/subprocess.html#popen-constructor)。

于 2012-07-12T18:21:19.190 回答