栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

subprocess.Popen stdin读取文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

subprocess.Popen stdin读取文件

如果您不加缓冲地打开文件,那么它将起作用:

import subprocesswith open('in.txt', 'rb', 0) as a, open('out.txt', 'w') as b:    header = a.readline()    rc = subprocess.call(['sort'], stdin=a, stdout=b)

subprocess
模块在文件描述符级别(操作系统的低级别无缓冲I /
O)工作。这可能与工作
os.pipe()
socket.socket()
pty.openpty()
,有什么有效
.fileno()
的方法,如果操作系统支持的话。

不建议在同一文件上混合使用缓冲和无缓冲的I / O。

在Python 2上,

file.flush()
导致输出出现,例如:

import subprocess# 2ndwith open(__file__) as file:    header = file.readline()    file.seek(file.tell()) # synchronize (for io.open and Python 3)    file.flush()# synchronize (for C stdio-based file on Python 2)    rc = subprocess.call(['cat'], stdin=file)

没有

subprocess
模块,可以复制此问题
os.read()

#!/usr/bin/env python# 2ndimport oswith open(__file__) as file: #XXX fully buffered text file EATS INPUT    file.readline() # ignore header line    os.write(1, os.read(file.fileno(), 1<<20))

如果缓冲区很小,那么将打印文件的其余部分:

#!/usr/bin/env python# 2ndimport osbufsize = 2 #XXX MAY EAT INPUTwith open(__file__, 'rb', bufsize) as file:    file.readline() # ignore header line    os.write(2, os.read(file.fileno(), 1<<20))

如果第一行的大小不能被整除,它将消耗更多的输入

bufsize

默认

bufsize
bufsize=1
(行缓冲)在我的机器上的行为类似:文件开头消失了-大约4KB。

file.tell()
报告所有缓冲区大小的第二行开头的位置。由于预读缓冲区错误(使用预期的第二行位置),在Python
2上使用
next(file)
而不是在我的机器上
file.readline()
导致
file.tell()
5K左右。
io.open()

file.seek(file.tell())
使用默认的基于stdio的文件对象在Python 2上尝试子进程调用前无济于事。它在Python
2上可与,模块中的
open()
功能一起使用
io
_pyio
在Python 3上可与默认
open
(也
io
基于)一起使用。

尝试

io
_pyio
关于Python 2和Python 3具有和不具有模块
file.flush()
产生不同的结果。它确认
在同一文件描述符上混合缓冲的I / O和非缓冲的I / O不是一个好主意



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/668986.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号