Python, how to get the details of a file

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

  • os.path.getsize() returns the size of the file
  • os.path.getmtime() returns the file last modified date
  • os.path.getctime() returns the file creation date (equals to last modified date in Unix systems like macOS)

Here 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() returns all the information you need in a concise way:

import os

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

print(os.stat(filename))

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:

  • 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)


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

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

  • os.path.getsize() returns the size of the file
  • os.path.getmtime() returns the file last modified date
  • os.path.getctime() returns the file creation date (equals to last modified date in Unix systems like macOS)

Here 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() returns all the information you need in a concise way:

import os

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

print(os.stat(filename))

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:

  • 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)


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-01-24T05:00:00+00:00) Python, how to get the details of a file. Retrieved from https://www.scien.cx/2021/01/24/python-how-to-get-the-details-of-a-file/

MLA
" » Python, how to get the details of a file." flaviocopes.com | Sciencx - Sunday January 24, 2021, https://www.scien.cx/2021/01/24/python-how-to-get-the-details-of-a-file/
HARVARD
flaviocopes.com | Sciencx Sunday January 24, 2021 » Python, how to get the details of a file., viewed ,<https://www.scien.cx/2021/01/24/python-how-to-get-the-details-of-a-file/>
VANCOUVER
flaviocopes.com | Sciencx - » Python, how to get the details of a file. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/01/24/python-how-to-get-the-details-of-a-file/
CHICAGO
" » Python, how to get the details of a file." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/01/24/python-how-to-get-the-details-of-a-file/
IEEE
" » Python, how to get the details of a file." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/01/24/python-how-to-get-the-details-of-a-file/. [Accessed: ]
rf:citation
» Python, how to get the details of a file | flaviocopes.com | Sciencx | https://www.scien.cx/2021/01/24/python-how-to-get-the-details-of-a-file/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.