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
Starting with python 3.5 the idiomatic solution would be:
import os
def absolute_file_paths(directory):
path = os.path.abspath(directory)
return [entry.path for entry in os.scandir(path) if entry.is_file()]
This not just reads nicer but also is faster in many cases. For more details (like ignoring symlinks) see original python docs: https://docs.python.org/3/library/os.html#os.scandir