Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 909
forvas Avatar asked Mar 02 '26 05:03

forvas


1 Answers

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!

like image 141
forvas Avatar answered Mar 03 '26 18:03

forvas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!