有没有办法遍历列表并分配一个变量

Is there a way to loop through a list, and assign a variable

我想做的是将变量 (a) 分配给 (b) 的每个实例。 (a) 中有 30 个文件夹,它应该如下所示: 'm:/groupfolder/susan/01' 'm:/groupfolder/susan/03' 'm:/groupfolder/susan/04' ...

这是我正在做的 -

import os
from os import listdir
a = ('m:/groupfolder/susan')
b = []
for x in os.listdir(a):
   b.append(x)

fha = [b]
for y in fha:
    path = a+b
 Error mesage = "TypeError: can only concatenate str (not "list") to str"

请让我知道我做错了什么 - 是的,我是 Python 的新手,并且仍在学习。 谢谢

而不是收集列表中的所有文件名然后再次迭代它。您只需一步即可完成:

import os                                                                                                                                                              

b = []                                                                                                                                                                 
for file in os.listdir(s): 
    print(file) 
    b.append(s+'/'+file) 

print(b) 

使用列表推导式获取完整文件路径列表:

import os
file_paths = [os.path.abspath(f) for f in os.listdir('dir_name')]
print(file_paths)