【问题标题】:How to create multiple file paths with nested directories?如何使用嵌套目录创建多个文件路径?
【发布时间】: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 循环是嵌套的,我想用这些路径做更多的事情,但我觉得我嵌套得越多,我就会遇到更多的问题.还有其他方法可以更紧凑地获取文件路径吗?

【问题讨论】:

    标签: python for-loop os.walk


    【解决方案1】:

    你可以这样做:

    import glob
    
    mydir = r '/home/'
    
    for filename in glob.iglob(mydir + '**/*.py', recursive = True):
      print(filename)
    

    【讨论】:

      【解决方案2】:

      你可以使用glob内置模块

      我有这个目录结构

      tree input_folder/
       input_folder
      └──  year=2020
         ├──  month=08
         │  ├──  day=02
         │  │  ├──  hour=03
         │  │  │  └──  input.txt
         │  │  ├──  hour=04
         │  │  │  └──  input.txt
         │  │  ├──  hour=05
         │  │  │  └──  input.txt
         │  │  └──  hour=06
         │  │     └──  input.txt
         │  └──  day=03
         │     ├──  hour=03
         │     │  └──  input.txt
         │     ├──  hour=04
         │     │  └──  input.txt
         │     ├──  hour=05
         │     │  └──  input.txt
         │     └──  hour=06
         │        └──  input.txt
         └──  month=09
            └──  day=02
               ├──  hour=03
               │  └──  input.txt
               └──  hour=04
                  └──  input.txt
      

      您可以使用此获取所有这些文件

      In [1]: from glob import glob
      
      In [2]: glob("input_folder/**/*.txt", recursive=True)
      Out[2]:
      ['input_folder/year=2020/month=09/day=02/hour=03/input.txt',
       'input_folder/year=2020/month=09/day=02/hour=04/input.txt',
       'input_folder/year=2020/month=08/day=03/hour=05/input.txt',
       'input_folder/year=2020/month=08/day=03/hour=03/input.txt',
       'input_folder/year=2020/month=08/day=03/hour=04/input.txt',
       'input_folder/year=2020/month=08/day=03/hour=06/input.txt',
       'input_folder/year=2020/month=08/day=02/hour=05/input.txt',
       'input_folder/year=2020/month=08/day=02/hour=03/input.txt',
       'input_folder/year=2020/month=08/day=02/hour=04/input.txt',
       'input_folder/year=2020/month=08/day=02/hour=06/input.txt']
      

      在你的情况下应该是

      glob(os.path.join(source,"**", "*.xml"), recursive=True)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-24
        • 2014-03-08
        • 1970-01-01
        • 1970-01-01
        • 2019-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多