【发布时间】:2021-09-30 07:04:09
【问题描述】:
我在名为apartments 的目录中有以下设置:
apartments:
|_Blue
|__apartmentBlue1.xml
apartmentBlue2.xml
apartmentBlue3.xml
nonsense.txt
|_Red
|_apartmentRed1.xml
apartmentRed2.xml
apartmentRed3.xml
|_nonsense
如果文件以.xml结尾,我正在尝试获取每个目录中每个文件的文件路径
这是我的代码:
source: c:\data\desktop\buildingX\appartments
for root, dirs, files in os.walk(source):
for file in files:
for diro in dirs:
if file.endswith('.xml'):
file_path = os.path.join(source, diro, file)
print(file_path)
这给了我想要的输出,但我担心我的 for 循环是嵌套的,我想用这些路径做更多的事情,但我觉得我嵌套得越多,我就会遇到更多的问题.还有其他方法可以更紧凑地获取文件路径吗?
【问题讨论】: