2

いくつかの引数を指定して*.EXEファイルを実行し、その結果をファイルに出力するテストスクリプトを作成中です。テストを正しく実行する*.shテストスクリプトが1つあります(ただし、さらにテストするには手動で更新する必要があります)。このスクリプトの行は次のようになります。

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)

比較するファイルを開いたところ、スクリプトが機能しているときに問題が発生しました。最初の(シェル)ソリューションは、正しい行末を出力します。2番目の(python)ソリューションは、CRCRLFで終わる行を出力します。これはメモ帳では正しいように見えますが、メモ帳++では、1行おきに空白のように見えます。

これらの行の間に空の行があってはなりません

Pythonからの出力で不適切な行末が表示されるのはなぜですか?

行末を修正するにはどうすればよいですか?(別のスクリプトを記述せずにCR CRLFをCRLFに変更します)

4

1 に答える 1

3

universal_newlines=Trueの引数を使用してみてください( http://docs.python.org/library/subprocess.html#popen-constructorPopenを参照)。

于 2012-07-12T18:21:19.190 に答える