We now have a youtube channel. Subscribe!

Python os.lstat() 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 lstat() method.

The Python os lstat() method is very similar to fstat() method and returns the information about a file, but do not follow symbolic link. This is also known as fstat() on platforms that do not support symbolic links, such as Windows. Below is the structure returned by lstat() 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 lstat() method -

os.lstat( path )

RECOMMENDED POST: Python os.lseek() Method

Parameter Details

  • path - This is the file path for which information would be returned.

Return Value

This returns information about a file.

Example

The following below is a simple example -

#!/usr/bin/python

import os, sys

# Open a file
path = "/var/www/html/foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )

# Close opened file
os.close( fd )

# Now get  the touple
info = os.lstat(path)

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

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

File Info : (33261, 3450178L, 103L, 1, 500, 500, 0L, 
             1238866944, 1238866944, 1238948312)
UID of the file :500
GID of the file :500


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 major() 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.

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.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain