Python, how to get detailed information of a file

Once the path of the file is given, you can use the following methods to provide more information about the file:osModule:

  • os.path.getsize()Returns the size of the file
  • os.path.getmtime()Returns the last modified date of the file
  • os.path.getctime()Returns the file creation date (equal to the last modified date in Unix systems such as macOS)

This is an example:

import os

filename = ‘/Users/flavio/test.txt’

print(os.path.getsize(filename)) print(os.path.getmtime(filename)) print(os.path.getctime(filename))

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

import os

filename = ‘/Users/flavio/test.txt’


It returns aos.stat_resultpurpose:

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:

  • st_mode the file type and permissions
  • st_ino the inode number
  • st_dev the device id
  • st_uid the file owner id
  • st_gid the file group id
  • st_size the file size

and you can reach for individual properties:

import os

filename = ‘/Users/flavio/test.txt’

stats = os.stat(filename)

print(stats.st_size) print(stats.st_mtime)

More python tutorials: