我正在尝试编写一个函数,该函数将遍历给定路径中的一系列子文件夹,并解压缩其中以.fastq.gz结尾的所有文件,并将其他文件单独留下。
我知道还有其他方法可以解决这个问题,但是我的程序中的一些更复杂的代码依赖于相同的原理我会遇到相同的错误消息,我认为这样会更好示例
def unzipdir(path):#walks through folders in the listed path and unzips files
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith((".fastq.gz")):
filepath = os.path.join(root, name)
x = "gzip ", "-d ", "{kwarg}".format(kwarg=filepath)
print([x])
subprocess.call([x])
subprocess.call("exit 1", shell=True)
错误消息......
$ ./test.py
[('gzip ', '-d ', '/nobackup/example/working/examplefile.fastq.gz')]
Traceback (most recent call last):
File "./test.py", line 76, in <module>
unzipdir('/nobackup/example/working')
File "./test.py", line 23, in unzipdir
subprocess.call([x])
File "/usr/lib64/python2.6/subprocess.py", line 478, in call
p = Popen(*popenargs, **kwargs)
File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1238, in _execute_child
raise child_exception
AttributeError: 'tuple' object has no attribute 'rfind'
任何帮助都会受到赞赏,因为我无法追查错误原因。
非常感谢。
答案 0 :(得分:1)
subprocess.call
的第一个参数应该是参数的可迭代(列表,元组等),每个参数必须是一个字符串。在subprocess.call([x])
中,基于上面的print
,我们可以看到x
是一个字符串元组;因此,您传递的是subprocess.call
字符串元组的列表,而不是可迭代的字符串。只需拨打subprocess.call(x)
即可(或者,如果您一直坚持传递列表,请subprocess.call(list(x))
)。