Get absolute paths of all files in a directory

后端 未结 11 2239
一向
一向 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:33

    All files and folders:

    x = [os.path.abspath(os.path.join(directory, p)) for p in os.listdir(directory)]
    

    Images (.jpg | .png):

    x = [os.path.abspath(os.path.join(directory, p)) for p in os.listdir(directory) if p.endswith(('jpg', 'png'))]
    

提交回复
热议问题