- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个 fifo:
mkfifo tofetch
我运行这段 python 代码:
fetchlistfile = file("tofetch", "r")
while 1:
nextfetch = fetchlistfile.readline()
print nextfetch
正如我所希望的那样,它在 readline 上停滞了。我跑:
echo "test" > tofetch
而且我的程序不再停止。它读取该行,然后继续无限循环。为什么没有新数据时它不会再次停止?
我也尝试查看“not fetchlistfile.closed”,我不介意在每次写入后重新打开它,但 Python 认为 fifo 仍然打开。
最佳答案
根据 readline 的文档,当且仅当您位于文件末尾时,它才返回空字符串。关闭与文件结尾不同。文件对象只会在您调用 .close() 时关闭。当您的代码到达文件末尾时,readline() 会一直返回空字符串。
如果您只是将文件对象用作迭代器,Python 将自动一次读取一行并在文件末尾停止。像这样:
fetchlistfile = file("tofetch", "r")
for nextfetch in fetchlistfile:
print nextfetch
echo "test"> tofetch
命令打开命名管道,向其中写入“test”,然后关闭管道的末端。因为管道的写入端关闭,读取端看到文件结束。
关于python - 让 readline 在 FIFO 上阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2406365/
这个问题在这里已经有了答案: What could be the reason that `require` doesn't work in some places? (3 个回答) 6 个月前关闭。
我正在使用读取行从维基百科获取一些文本。但读取行仅返回列表,而不是我想要的文本。有什么方法可以使用替代方案或解决我的问题吗? public class mediawiki { public s
我正在编写一小段代码,其中涉及使用子进程运行一个脚本来监听一些实时数据 这是我的代码: def subscriber(): try: sub = subprocess.Pope
我已包括: #include "stdio.h" #include #include 我的编译器包含标志 -lreadline 但我仍然收到错误消息: fatal error: 'readl
使用 Term::Readline::readline 停止无限循环的正确方法是什么? ? 这样我一个都看不懂 0 #!/usr/bin/env perl use warnings; use stri
标题比我的实际目标更具体: 我有一个使用 GNU Readline 的命令行程序,主要用于命令历史记录(即使用向上箭头检索以前的命令)和其他一些细节。现在,程序的输出似乎散布在用户的输入中,有时是可以
在 ipython 中,如果我按“esc”,然后按“enter”(可能还有其他字符?),读行会中断。我无法再使用“向上”键搜索命令历史记录,并且某些命令(例如 control-K)失败。 有没有办法在
我在使用 readlines() 和 readline() 返回值时遇到问题,但在使用 read() 时却没有。任何人都知道这是怎么发生的?欣赏一下 with open('seatninger.txt
标题比我的实际目标更具体: 我有一个使用 GNU Readline 的命令行程序,主要用于命令历史记录(即使用向上箭头检索以前的命令)和其他一些细节。现在,程序的输出似乎散布在用户的输入中,有时是可以
我正在编写一个聊天客户端,它必须在接收用户输入的同时输出接收到的消息。 到目前为止,我已经 fork 成两个独立的进程,其中一个继续监听套接字连接并用 printf 写出接收到的字符串。另一个使用 r
我在 NetworkStream 上使用 StreamReader,我只想读取一行或多行,而另一个数据是 byte array(如文件数据)我不想在 StreamReader 中读取该文件数据,例如我
我遇到了这两个 API,用于在 C# 的简单控制台应用程序中读取用户的输入: System.Console.ReadLine() System.Console.In.ReadLine() 这是一个我试
yum 我的系统显示已安装 readline rlwrap-0.41]$ sudo yum install readline Loaded plugins: fastestmirror, presto
我尝试做 this tutorial在 Rust 中,到目前为止,我在将 C 库连接到 Rust 时遇到了很多问题。 C 等效代码: #include #include #include #in
我正在寻找 web Python的标题中提到的命令及其区别;但是,我并不满足于对这些命令有完整的基本理解。 假设我的文件只有以下内容。 This is the first time I am posi
你如何在 F# 中使用 Console.Readline?与 Console.Writeline 不同,当我调用它时,它并没有受到尊重。 最佳答案 如果你使用 let s = Console.Read
在一次面试中,面试官问我为什么 readline() 比 Python 中的 readlines() 慢很多? 我回答的是readlines()需要多次读取,需要更多的开销。 不知道我的回答对不对。
要在 OSX Lion 上完全运行 ipython 需要什么?我试图让 ipython 与 readline 一起工作,但没有成功。 我的做法: (在虚拟环境中) pip install ipytho
在 Nodejs 文档中,我看到: import EventEmitter from 'events'; import { readFile } from 'fs'; import fs, { rea
我写了一个简单的应用程序: #include #include #include #include int main() { char *user_input; while(u
我是一名优秀的程序员,十分优秀!