[英]How can i have multiple loop to reach list of all files from multiple determined folders?
I have multiple folders.我有多个文件夹。 I will read some of them;我会读一些; each folder includes various files (images) that I want access to their paths.每个文件夹都包含我想要访问其路径的各种文件(图像)。 I used the below code:我使用了以下代码:
[os.path.join(folder,fname) for fname in os.listdir(folder) for folder in selected_train]
previously I had a list of folders in folders.以前我有一个文件夹列表。 but I get the below error:但我收到以下错误:
name 'folder' is not defined
How can I correct it?我该如何纠正?
You can use os.walk
function to navigate through the folder.您可以使用os.walk
function 浏览文件夹。
import os
subfiles=[x[2] for x in os.walk(".")]
print(subfiles)
And also, if you want to just one list you can do this;而且,如果您只想列出一个列表,您可以这样做;
import os
subfiles=[]
for x in os.walk("."):
subfiles.extend(x[2])#x[2] represent the all files in one directory
print(subfiles)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.