如预期subprocess.call不起作用 [英] subprocess.call doesn't work as expected
问题描述
我有以下批处理文件(test.bat的)
I have the following batch file (test.bat)
my.py < commands.txt
my.py执行以下操作:
my.py does the following:
import sys
print sys.stdin.readlines()
一切工作正常,如果我(在Windows 7从cmd.exe的外壳)启动命令行该批处理文件。
Everything works fine if I launch this batch file from command line (from cmd.exe shell in Windows 7).
但是,如果我尝试通过 subprocess.call
从蟒蛇运行它的功能就无法正常工作。
But if I try to run it via subprocess.call
function from python it doesn't work.
我如何尝试从蟒蛇运行:
How I try to run it from python:
import subprocess
import os
# Doesn't work !
rc = subprocess.call("test.bat", shell=True)
print rc
这是我得到的错误消息:
And this is the error message that I get:
>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我获得相同的行为。
I'm using python 2.7.2 but on 2.7.5 I get the same behavior.
任何想法?
推荐答案
这是否工作:
from subprocess import *
rc = Popen("test.bat", shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
print rc.stdout.readline()
print rc.stderr.readline()
rc.stdout.close()
rc.stdin.close()
rc.stderr.close()
我不知道为什么你参考一下:
I'm not sure why you refer to:
my.py < commands.txt
是否输入有什么与蝙蝠文件?
如果是这样你拨打:
Does the input has anything to do with the bat-file? If so do you call:
python my.py
它通过子,做打开batfile
which opens the batfile via subprocess that does:
batfile < commands.txt
为什么是相关的?
or why is that relevant?
这篇关于如预期subprocess.call不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!