Python 3 – os.stat() Method
Python is a high-level programming language that is used in many applications such as software development, data analysis, and web development. The os module is one of the built-in modules in Python that provides a way of using operating system functionalities. The os.stat() is one of the powerful methods of the os module that provides detailed information about a file in the operating system. In this article, we will discuss the os.stat() method, its syntax, and working with sample codes.
Introduction to os.stat() Method
Python os.stat() method returns a tuple containing information about a file or a directory. This includes file size, creation time, modification time, and access time, etc. The method can also reveal information about the owner and group of a file and file permissions.
The os.stat() method works by taking in the path of a file or a directory and then returns a tuple that contains the information about the file. In more technical terms, it provides an interface to the stat() system call.
Syntax of os.stat() Method
The syntax of os.stat() method is given below:
os.stat(path)
Where path is the file path of the file or directory we are interested in.
Understanding the Tuple returned by os.stat() Method
The os.stat() method returns a tuple containing the following values:
- st_mode – The file permissions which are represented as a bit field.
- st_ino – The inode number of the file.
- st_dev – The identifier of the device on which the file resides.
- st_nlink – The number of hard links to the file.
- st_uid – The user identifier of the file owner.
- st_gid – The group identifier of the file’s group owner.
- st_size – The size of the file in bytes.
- st_atime – The last access time of the file.
- st_mtime – The last modification time of the file.
- st_ctime – The creation time of the file.
Working with os.stat() Method
Let us now see some examples of working with os.stat() method. The os.stat()
method is used to get detailed information about the file or directory. We can extract different information like file permissions, size, creation time, modification time, or even the owner of the file. In the following examples, we will use os.stat() method to extract different information about files and directories.
Example 1: Using os.stat to Get File Size
import os
file_stats = os.stat('/path/to/file')
print(f"File size in bytes: {file_stats.st_size}")
Output:
File size in bytes: 1423
Here we use the os.stat() method to get the information about the file of path /path/to/file
. We then extract the size of the file using the file_stats.st_size
line.
Example 2: Using os.stat to Get File Permissions
import os
file_stats = os.stat('/path/to/file')
print(f"File Permissions: {oct(file_stats.st_mode)[-3:]}")
Output:
File Permissions: 755
In this example, we use os.stat() method to get the information about the file of path /path/to/file
. We then extract the permissions of the file using the oct(file_stats.st_mode)[-3:]
line. We use the octal function to convert the file permission from decimal to octal.
Example 3: Using os.stat to Get The Last Modified Time
import os
from datetime import datetime
file_stats = os.stat('/path/to/file')
modification_time = file_stats.st_mtime
print(f"Last Modified Time: {datetime.utcfromtimestamp(modification_time)}")
Output:
Last Modified Time: 2022-01-01 14:33:19
Here we use os.stat() method to get the information about the file of path /path/to/file
. We then extract the modification time of the file using the file_stats.st_mtime
line. We convert this time to human-readable format using the datetime module in Python.
Conclusion
The os.stat() method is a powerful method in Python that provides detailed information about a file or directory. The method enables us to extract various information about a file or directory, including permission, size, creation time, modification time, and even the owner of the file. In summary, the os.stat() method provides us with the ability to extract detailed information about a file or directory, which is essential in many applications.