Thursday, August 6, 2020

Python os.fstat() Method with example



Hello dear readers! Welcome back to another section of our tutorial on Python. In this tutorial guide, we are going to be studying about the Os fstat() method.

The Python os fstat() method gives back the information about a file that is associated with the fd. Here is the structure returned by the fstat method -

  • st_dev - ID of the device containing file.
  • st_mode - protection
  • st_nlink - the number of hard links
  • st_uid - user ID of owner
  • st_gid - group ID of owner
  • st_rdev - device ID (if special file)
  • st_size - total size, in bytes
  • st_blksize - blocksize for file system I/O
  • st_blocks - number of blocks being allocated
  • st_atime - the time of last access
  • st_mtime - the time of last modification
  • st_ctime - time of last status change

Syntax

The following below is the syntax for Python Os fstat() method -

os.fstat( fd )


Parameter Details

  • fd - File descriptor for which system information is to be returned.

Return Value

This returns information about a file associated with the fd.

Example

The following below is a simple example -

#!/usr/bin/python

import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Now get  the touple
info = os.fstat(fd)

print "File Info :", info

# Now get uid of the file
print "UID of the file :%d" % info.st_uid

# Now get gid of the file
print "GID of the file :%d" % info.st_gid

# Close opened file
os.close( fd)

Output
When the above code is executed, it will produce the following result -

File Info : (33261, 3753776L, 103L, 1, 0, 0, 
            102L, 1238783197, 1238786767, 1238786767)
UID of the file :0
GID of the file :0


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the Python OS fstatvfs() Method.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.