如果您不加缓冲地打开文件,那么它将起作用:
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不是一个好主意 。



