Get absolute paths of all files in a directory

后端 未结 11 2238
一向
一向 2020-12-12 19:00

How do I get the absolute paths of all the files in a directory that could have many sub-folders in Python?

I know os.walk() recursively gives me a list

11条回答
  •  春和景丽
    2020-12-12 19:35

    os.path.abspath makes sure a path is absolute. Use the following helper function:

    import os
    
    def absoluteFilePaths(directory):
       for dirpath,_,filenames in os.walk(directory):
           for f in filenames:
               yield os.path.abspath(os.path.join(dirpath, f))
    

提交回复
热议问题