【发布时间】:2015-05-28 12:36:43
【问题描述】:
当我开始询问上一个问题时,我正在使用 python 的 tarfile 模块提取一个 tarball。我不希望将提取的文件写入磁盘,而是直接通过管道传输到另一个程序,特别是 bgzip。
#!/usr/bin/env python
import tarfile, subprocess, re
mov = []
def clean(s):
s = re.sub('[^0-9a-zA-Z_]', '', s)
s = re.sub('^[^a-zA-Z_]+', '', s)
return s
with tarfile.open("SomeTarballHere.tar.gz", "r:gz") as tar:
for file in tar.getmembers():
if file.isreg():
mov = file.name
proc = subprocess.Popen(tar.extractfile(file).read(), stdout = subprocess.PIPE)
proc2 = subprocess.Popen('bgzip -c > ' + clean(mov), stdin = proc, stdout = subprocess.PIPE)
mov = None
但现在我陷入了困境:
Traceback (most recent call last):
File "preformat.py", line 12, in <module>
proc = subprocess.Popen(tar.extractfile(file).read(), stdout = subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 36] File name too long
有什么解决方法吗?我一直在使用LightTableLinux.tar.gz(它包含文本编辑器程序的文件)作为压缩包来测试它的脚本。
【问题讨论】:
-
Popen(tar.extractfile(file).read())您正在传递 tarfile 中特定文件的 contents 作为要执行的程序名称。