What causes "AttributeError: 'tuple' object has no attribute 'rfind'" when I attempt to use subprocess.call
What causes "AttributeError: 'tuple' object has no attribute 'rfind'" when I attempt to use subprocess.call
我正在尝试编写一个函数,它将遍历给定路径中的一系列子文件夹,并解压缩其中以 .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'
非常感谢您的帮助,因为我无法找到错误的原因。
非常感谢。
subprocess.call
的第一个参数应该是参数的可迭代(列表、元组等),并且每个参数必须是字符串。在subprocess.call([x])
中,根据上面的print
,我们可以看出x
是一个字符串元组;因此,您传递的是 subprocess.call
一个字符串元组列表,而不是一个可迭代的字符串。只需调用 subprocess.call(x)
(或者,如果您坚持要传递列表,则调用 subprocess.call(list(x))
)。
我正在尝试编写一个函数,它将遍历给定路径中的一系列子文件夹,并解压缩其中以 .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'
非常感谢您的帮助,因为我无法找到错误的原因。
非常感谢。
subprocess.call
的第一个参数应该是参数的可迭代(列表、元组等),并且每个参数必须是字符串。在subprocess.call([x])
中,根据上面的print
,我们可以看出x
是一个字符串元组;因此,您传递的是 subprocess.call
一个字符串元组列表,而不是一个可迭代的字符串。只需调用 subprocess.call(x)
(或者,如果您坚持要传递列表,则调用 subprocess.call(list(x))
)。