[英]Is there a way to loop through a list, and assign a variable
What I'd like to do is assign variable (a) to each instance of (b).我想做的是将变量(a)分配给(b)的每个实例。 There are 30 folders within (a) It should look like the following: 'm:/groupfolder/susan/01' 'm:/groupfolder/susan/03' 'm:/groupfolder/susan/04'...
(a) 中有 30 个文件夹 它应该如下所示: 'm:/groupfolder/susan/01' 'm:/groupfolder/susan/03' 'm:/groupfolder/susan/04'...
Here is what I'm doing -这就是我正在做的 -
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"
Please let me know what I'm doing wrong - yes I'm relatively new to Python and still learning.请让我知道我做错了什么 - 是的,我对 Python 还比较陌生,并且还在学习。 Thanks
谢谢
Instead of collecting all filename in list and then iterating it again.而不是收集列表中的所有文件名然后再次迭代它。 You can simply do it in one step:
您可以简单地一步完成:
import os
b = []
for file in os.listdir(s):
print(file)
b.append(s+'/'+file)
print(b)
Use a list comprehension to get a list of full file paths:使用列表推导获取完整文件路径的列表:
import os
file_paths = [os.path.abspath(f) for f in os.listdir('dir_name')]
print(file_paths)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.