在Windows 7下将文本文件用作python中的stdin [英] Using textfile as stdin in python under windows 7

查看:154
本文介绍了在Windows 7下将文本文件用作python中的stdin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是win7用户.

我不小心阅读了* nix系统中的重定向(例如command1 < infile > outfile),然后发现

I accidentally read about redirections (like command1 < infile > outfile) in *nix systems, and then I discovered that something similar can be done in Windows (link). And python is also can do something like this with pipes(?) or stdin/stdout(?).

我不了解Windows中的情况,所以我有一个问题.

I do not understand how this happens in Windows, so I have a question.

我使用某种专有的Windows程序(.exe).该程序能够将数据附加到文件中. 为简单起见,我们假设它等同于

I use some kind of proprietary windows-program (.exe). This program is able to append data to a file. For simplicity, let's assume that it is the equivalent of something like

while True:
    f = open('textfile.txt','a')
    f.write(repr(ctime()) + '\n')
    f.close()
    sleep(100)

问题: 我可以将此文件(textfile.txt)用作stdin吗? 我的意思是,脚本(在运行时)应始终(而不是一次)处理所有新数据,即

The question: Can I use this file (textfile.txt) as stdin? I mean that the script (while it runs) should always (not once) handle all new data, ie

在永无止境的周期"中:

In the "never-ending cycle":

  • 程序(.exe)写一些东西.

  • The program (.exe) writes something.

Python脚本捕获数据和进程.

Python script captures the data and processes.

您能用python还是在win cmd/.bat或其他方式中写出如何做到这一点.

Could you please write how to do this in python, or maybe in win cmd/.bat or somehow else.

这真是太酷了.我想学习如何做! :D

推荐答案

如果我正确阅读了您的问题,那么您希望将输出从一个命令传递到另一个命令.

If I am reading your question correctly then you want to pipe output from one command to another.

通常是这样完成的:

cmd1 | cmd2

但是,您说您的程序仅写入文件.我会仔细检查文档,看看它们是否不是让命令写到stdout而不是文件的一种方法.

However, you say that your program only writes to files. I would double check the documentation to see if their isn't a way to get the command to write to stdout instead of a file.

如果这不可能,那么您可以创建称为named pipe的内容.它在文件系统上显示为文件,但实际上只是可以写入和读取的数据缓冲区(数据是流,只能读取一次).意味着直到程序写入管道停止写入并关闭文件",您的程序读取操作才会结束.我没有在Windows上使用命名管道的经验,因此您需要为此提出一个新问题.管道的一个缺点是它们的缓冲区大小有限.因此,如果没有程序从管道中读取数据,则一旦缓冲区已满,写入程序将无法继续执行,并且会无限期地等待程序开始从管道中读取数据.

If this is not possible then you can create what is known as a named pipe. It appears as a file on your filesystem, but is really just a buffer of data that can be written to and read from (the data is a stream and can only be read once). Meaning your program reading it will not finish until the program writing to the pipe stops writing and closes the "file". I don't have experience with named pipes on windows so you'll need to ask a new question for that. One down side of pipes is that they have a limited buffer size. So if there isn't a program reading data from the pipe then once the buffer is full the writing program won't be able to continue and just wait indefinitely until a program starts reading from the pipe.

另一种选择是,在Unix上,有一个名为tail的程序,可以将其设置为连续监视文件的更改,并输出附加到文件的任何数据(延迟很短).

An alternative is that on Unix there is a program called tail which can be set up to continuously monitor a file for changes and output any data as it is appended to the file (with a short delay.

tail --follow=textfile.txt --retry | mycmd 
# wait for data to be appended to the file and output new data to mycmd

cmd1 >> textfile.txt # append output to file

要注意的一件事是,tail不会仅仅因为第一个命令已停止写入文件而停止. tail将继续永久监听该文件上的更改,或者直到mycmd停止监听tail,或者直到tail被杀死(或签名")为止.

One thing to note about this is that tail won't stop just because the first command has stopped writing to the file. tail will continue to listen to changes on that file forever or until mycmd stops listening to tail, or until tail is killed (or "sigint-ed").

问题有各种答案关于如何在Windows计算机上获取tail版本的信息.

This question has various answers on how to get a version of tail onto a windows machine.

这篇关于在Windows 7下将文本文件用作python中的stdin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆