← flaviocopes.com

Python, how to get the details of a file

Published Jan 24 2021

Given the path to a file, you can get more information about it using several method provided by the os module:

Here is an example:

import os

filename = '/Users/flavio/test.txt'


os.stat() returns all the information you need in a concise way:

import os

filename = '/Users/flavio/test.txt'


It returns a os.stat_result object:

os.stat_result(st_mode=33252, st_ino=34409711, st_dev=16777224, st_nlink=1, st_uid=501, st_gid=20, st_size=189, st_atime=1605428774, st_mtime=1605428773, st_ctime=1605428773)

We have a lot of information here, including:

and you can reach for individual properties:

import os

filename = '/Users/flavio/test.txt'

stats = os.stat(filename)


Wanna go from noobie to expert?

I wrote an entire book on this topic 👇

© 2023 Flavio Copes Flavio Copes using Notion to Site Notion to Site. Follow on Twitter

Solopreneur? Wannabe? Adventure awaits