Python os.lseek() Method


Description

Python method lseek() sets the current position of file descriptor fd to the given position pos, modified by how.

Syntax

Following is the syntax for lseek() method −

os.lseek(fd, pos, how)

Parameters

Return Value

This method does not return any value.

Example

The following example shows the usage of lseek() method.

#!/usr/bin/python

import os, sys

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

# Write one string
os.write(fd, "This is test")

# Now you can use fsync() method.
# Infact here you would not be able to see its effect.
os.fsync(fd)

# Now read this file from the beginning
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print "Read String is : ", str

# Close opened file
os.close( fd )

print "Closed the file successfully!!"

When we run above program, it produces following result −

Read String is :  This is test
Closed the file successfully!!
python_files_io.htm