这是我的第一个问题,我是一个Python新手。
使用Python3.5。在
我想递归地使用glob,发现这个关于递归使用glob的答案非常有用:Use a Glob() to find files recursively in Python?
但是,我还想使用循环变量,似乎无法找到正确的语法。 这将返回一个很好的列表:
x = glob.glob(('{0}*.txt').format(some_variable))
这是由对环球网上面我链接到的问题,效果很好,并返回一个列表:
^{pr2}$到目前为止,还不错。但当我尝试此操作时,我得到“AttributeError:'list'object has no attribute'format'”:
x = glob.glob('**/{0}*.txt', recursive = True).format(some_variable)
我也试过了,这会产生和“无效语法”错误:
x = glob.glob(('**/{0}*.txt').format(some_variable)), recursive = True)
我敢打赌我在一个非常基本的意义上出错了,或者我只是不能使用我在这里尝试的glob递归版本的.format语法?我认为还有其他递归的方法,但是我仍然不明白为什么我尝试的第一个语法不起作用。在
如有任何意见,我们将不胜感激!谢谢!在
试试这个:
相关问题 更多 >
编程相关推荐