Python os.lseek() Method
Example
Read file from the start:
#Import os Library
import os
# open a file and create a file descriptor
fd = os.open( "test.txt", os.O_RDWR|os.O_CREAT)
#write something to file
os.write(fd, "This is a test message".encode())
#Read file from the start
os.lseek(fd, 0, 0)
print ("Read file from here:",os.read(fd, 100).decode())
#close file
os.close(fd)
Definition and Usage
The os.lseek()
method sets the current position of file descriptor to the defined position (beginning of the file, current position or end of the file).
os.lseek()
return the new cursor position in bytes, starting from the beginning.
Note: Available on WINDOWS and UNIX platforms.
Syntax
os.lseek(fd, pos, how)
Parameter Values
Parameter | Description |
---|---|
fd | Required. Represents a file descriptor whose position is to be set |
pos | Required. Represents position to set in the file, there are 3 fixed parameters os.SEEK_SET or 0 - set position relative to the beginning of the fileos.SEEK_CUR or 1 - set position relative to the current positionos.SEEK_END or 2 - set position relative to the end of the file |
how | Required. Represents reference point in the file, there are 3 parameters same as pos os.SEEK_SET or 0 - set position relative to the beginning of the file os.SEEK_CUR or 1 - set position relative to the current position os.SEEK_END or 2 - set position relative to the end of the file |
Technical Details
Return Value: | None |
---|---|
Python Version: | pre - 2.6 |