简体   繁体   English

你可以在Python的MS Windows上打开stdin作为文件吗?

[英]Can you open stdin as a file on MS Windows in Python?

On Linux, I'm using supbprocess.Popen to run an app. 在Linux上,我使用supbprocess.Popen来运行一个应用程序。 The command line of that app requires a path to an input file. 该应用程序的命令行需要输入文件的路径。 I learned I can pass the path /dev/stdin to the command line, and then use Python's subproc.stdin.write() to send input to the subprocess. 我了解到我可以将路径/ dev / stdin传递给命令行,然后使用Python的subproc.stdin.write()将输入发送到子进程。

import subprocess
kw['shell'] = False
kw['executable'] = '/path/to/myapp'
kw['stdin'] = subprocess.PIPE
kw['stdout'] = subprocess.PIPE
kw['stderr'] = subprocess.PIPE
subproc = subprocess.Popen(['','-i','/dev/stdin'],**kw)
inbuff = [u'my lines',u'of text',u'to process',u'go here']
outbuff = []
conditionbuff = []

def processdata(inbuff,outbuff,conditionbuff):
    for i,line in enumerate(inbuff):
        subproc.stdin.write('%s\n'%(line.encode('utf-8').strip()))
        line = subproc.stdout.readline().strip().decode('utf-8')
        if 'condition' in line:
            conditionbuff.append(line)
        else:
            outbuff.append(line)

processdata(inbuff,outbuff,conditionbuff)

There's also an MS Windows version of this app. 这个应用程序还有一个MS Windows版本。 Is there an equivalent on MS Windows to using the /dev/stdin or is the a Linux (Posix) specific solution? 在MS Windows上是否有使用/ dev / stdin的等价物,或者是Linux(Posix)特定的解决方案?

你不应该使用'/dev/stdin'而是sys.stdin

If myapp treats - as a special filename that denotes stdin then: 如果myapp处理-作为表示stdin的特殊文件名,则:

from subprocess import PIPE, Popen

p = Popen(['/path/to/myapp', '-i', '-'], stdin=PIPE, stdout=PIPE)
stdout, _ = p.communicate('\n'.join(inbuff).encode('utf-8'))
outbuff = stdout.decode('utf-8').splitlines()

If you can't pass - then you could use a temporary file: 如果你不能通过-那么你可以使用一个临时文件:

import os
import tempfile

with tempfile.NamedTemporaryFile(delete=False) as f:
     f.write('\n'.join(inbuff).encode('utf-8'))

p = Popen(['/path/to/myapp', '-i', f.name], stdout=PIPE)
outbuff, conditionbuff = [], []
for line in iter(p.stdout.readline, ''):
    line = line.strip().decode('utf-8')
    if 'condition' in line:
        conditionbuff.append(line)
    else:
        outbuff.append(line)
p.stdout.close()
p.wait()
os.remove(f.name) #XXX add try/finally for proper cleanup

To suppress stderr you could pass open(os.devnull, 'wb') as stderr to Popen . 为了抑制stderr你可以将open(os.devnull, 'wb')作为stderr传递给Popen

Total shot in the dark, but con: is the name of the console device (you know, good ol' copy con newfile.txt from DOS). 黑暗中的总镜头,但是con:是控制台设备的名称(你知道,来自DOS的好的' copy con newfile.txt )。 You might be able to pass con: as an argument to the program that won't accept '-'. 您可以将con:作为参数传递给不接受' - '的程序。

Please, use 请用

import sys 导入系统

sys.stdout.write("Abc\\n123\\n") sys.stdout.write函数( “ABC \\ N123 \\ n” 个)

or 要么

for line in sys.stdin.readlines(): 对于sys.stdin.readlines()中的行:

... print line ...打印线

for example 例如

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows上为python打开文件? 遇到意外问题 - How do you open a file on windows for python? having unexpected issues 你如何在Windows PowerShell中打开一个notepad ++ python文件? - How do you open a notepad++ python file in windows powershell? 如何在 Python 中使用文件作为标准输入? - How can I use a file as stdin in Python? 有没有办法在 python 中发送电子邮件而 Windows 询问您“您想如何打开此文件”? - Is there a way to send emails in python without Windows asking you "How you want to open this file"? 如何在Windows中使用python打开文件? - how to open file in windows with python? MS ACCESS Compact and Repair using Python 给出错误文件无法以独占方式打开 - MS ACCESS Compact and Repair using Python is giving the error file can not open exclusively 无法打开 python 脚本,要求选择您希望如何打开此文件 - Can't open python scripts, asks to choose how would you like to open this file 如何使用while循环,以便用户可以打开新的文本文件? 蟒蛇 - How can you use a while loop so the user can open a new text file? Python 您可以使用 python 打开命令提示符吗? - can you open command prompt with python? 你能在 Atom 编辑器中打开 Python shell 吗? - Can you open a Python shell in Atom editor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM