#!/usr/bin/env python3
import gzip
import subprocess
import shutil
filename = 'testfile'
data = 'Hello\n'
# Create some test files
with open(filename, 'w') as f:
f.write(data)
with gzip.open(filename + '.gz', 'wt') as f:
f.write(data)
# Make sure we can read them:
with open(filename) as f:
d = f.read()
print(d == data, d)
with gzip.open(filename + '.gz', 'rt') as f:
d = f.read()
print(d == data, d)
# See what Popen does with the files given as stdin
with open(filename) as f:
result = subprocess.Popen('hexdump -C'.split(), stdin=f,
stdout=subprocess.PIPE)
print(result.stdout.read().decode())
with gzip.open(filename + '.gz') as f:
result = subprocess.Popen('hexdump -C'.split(), stdin=f,
stdout=subprocess.PIPE)
print(result.stdout.read().decode())
# Fix the problem like this...
p = subprocess.Popen('hexdump -C'.split(),
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
with gzip.open(filename + '.gz', 'rb') as f:
with p.stdin:
shutil.copyfileobj(f, p.stdin)
print(p.stdout.read().decode())
# You can use the same pattern for the regular files, too:
p = subprocess.Popen('hexdump -C'.split(),
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
with open(filename, 'rb') as f:
with p.stdin:
shutil.copyfileobj(f, p.stdin)
print(p.stdout.read().decode())
subprocess.Popen(args, stdout=stdoutFile, stderr=stderFile,, stdin=subprocess.PIPE)
with gzip.open(GZFILE) as f:
shutil.copyfileobj(f, p.stdin)
# p.stdin.close() # to denote the end of the file.
正如@falsetru所说,Popen使用原始gzip数据,而不是解压缩的数据。下面是一个完整的例子:
输出:
^{pr2}$如果传递
gzip.open(..)
的返回值,则其内部文件描述符将传递给该文件;从子进程读取它将返回原始数据,而不是解压缩的数据。在您需要将解压后的数据传递给子进程的标准输入:
相关问题 更多 >
编程相关推荐