0

次のバッチ ファイル (test.bat) があります。

my.py < commands.txt

my.py は次のことを行います。

import sys

print sys.stdin.readlines()

このバッチ ファイルをコマンド ラインから (Windows 7 の cmd.exe シェルから) 起動すると、すべて正常に動作します。

しかし、Python から関数を介して実行しようとすると、subprocess.call機能しません。

Pythonから実行しようとする方法:

import subprocess
import os

# Doesn't work !
rc = subprocess.call("test.bat", shell=True)
print rc

そして、これは私が得るエラーメッセージです:

>my.py  0<commands.txt
Traceback (most recent call last):
  File "C:\Users\.....\my.py
", line 3, in <module>
    print sys.stdin.readlines()
IOError: [Errno 9] Bad file descriptor
1

私はpython 2.7.2を使用していますが、2.7.5でも同じ動作をします。

何か案は?

4

2 に答える 2

4

そのはず:

rc = subprocess.call(["cmd", "/c", "/path/to/test.bat"])

またはシェルを使用して:

rc = subprocess.call("cmd /c /path/to/test.bat", shell=True)
于 2013-05-20T09:04:05.437 に答える