| by Arround The Web | No comments

Python File Stat()

In Python, the “os” module provides several methods that are used to interact with the operating system. It also offers various methods for working with the files in Python. For instance, the operations such as renaming, deleting, moving, and others are performed using various “os” module functions. The “os.stat()” method of the “os” module is used in Python to get all the file information.

This blog explains an in-depth guide on the “os.stat()” method of the “os” module via the below content:

What is the “os.stat()” Method in Python?

In Python, the “os.stat()” method is utilized to retrieve all the file information, such as type of file, size of file, and others. This method is invoked on the specified path and retrieves the status or information of the file.

Syntax

os.stat(path)

 

Parameter

In the above syntax:

  • The “path” parameter indicates the bytes or string objects describing an absolute path.

Return Type

The “os.stat()” method retrieves a “stat_result” object containing all the information of the file. Some of the attributes of this object are illustrated below:

  • The “st_mode” attribute specifies the type and mode bits (permissions) of the file.
  • The “st_ino” attribute specifies the Unix inode number and Windows file index.
  • The “st_dev” attribute indicates the device identifier on which this specified file resides.
  • The “st_nlink”, “st_uid”, and “st_gid” attributes indicate the hard links number, user, and group identifier of the file owner.
  • The “st_size”, “st_atime”, and “st_mtime” attributes specify file size, recent access time, and recent content modification time.
  • The other attributes such as “st_ctime”, “st_atime_ns”, “st_mtime_ns”, “st_ctime_ns”, and others can be used to represent specific information of the file.

Now, let’s start with the working of this method using the below examples:

Example 1: Getting All Information of File Using “os.stat()” Method

The following code gets all the information of the specified file:

import os
path =r'C:\Users\p\Documents\program\new.csv'
print(os.stat(path))

 

In the above syntax:

  • The “os” module is imported.
  • The absolute path with the file name and format is defined and assigned to a variable “path”.
  • The “os.stat()” method takes the “path” as an argument and retrieves all the information of the file.

Output

The detailed information of the specified file has been retrieved successfully.

Example 2: Getting Specific Information of File Using “os.stat()” Method

We can also utilize the “os.stat()” method to retrieve the specific information of the file. Here is an example code:

import os
import time
path =r'C:\Users\p\Documents\program\new.csv'
z = os.stat(path)
print('File Mode Bit: ', z.st_mode)
print('Size of File (Bytes): ', z.st_size)
print('Recent Modification Time: ', time.ctime(z.st_mtime))

 

In the above code:

  • The “os” and “time” modules are imported.
  • The complete path of the specified file is defined and assigned to the variable “path”.
  • The “os.stat()” method takes the file path as an argument and returns the “stat_result” object containing all the information of the file.
  • The attribute “st_mode”, “st_size”, and “st_mtime” is used to get the file mode bits, size of the file, and recent modification time.
  • The recent modification time retrieves in seconds, so we convert it into a proper format using the “time.ctime()” method of the “time” module.

Output

The specified information of the file returns in the above output.

Conclusion

The “os.stat()” method of the “os” module in Python is utilized to retrieve all the file information such as type of file, size of file, and others. The “os.stat()” method accepts the complete path and retrieves all the file information via the “stat_result” object. The particular information related to the file can also be retrieved using the “os.stat()” method. This Python tutorial delivered a detailed guide on Python’s “os.stat()” method using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply