Search code examples
pythonlistfileabsolute-pathuid

Getting a list of all files (with absolute path) recursively and their uid with Python


I am trying to write a python script to list all files with absolute path in a given directory recursively and list their UID and file owners in front of them. (something like: ls -lR ) I wrote this one but it gives me an error at the end of execution:

import os
for folder, subfolders, files in os.walk(os.getcwd()):
    for file in files:
        filePath = os.path.abspath(file)
        print(filePath, os.stat(file).st_uid)

Solution

  • import os
    import glob
    
    for filename in glob.iglob('./**/*', recursive=True):
        print(os.path.abspath(filename), os.stat(filename).st_uid)
    

    Needs Python 3.5 or higher Use a Glob() to find files recursively in Python?