Search code examples
pythonpython-3.xlistfilestat

How to get file info in Python3 using os.listdir?


I simplified the problem. Now I have only this code:

files = os.listdir(dir_path)
print(files)

for f in files:
    info = os.stat(f)
    print(info.st_mtime)

Which gives me this error:

OSError: [Errno 2] No such file or directory: anyfile.txt

And print(files) is returning the files which I want. Why can't I access to their properties?

Thank you in advance, I am a bit lost with this point.


Solution

  • Ok, I got the solution... I only had to change this line:

    info = os.stat(dir_path + f)
    

    What a waste of time! Sorry for my stupid question!