Python3 os.lseek() Method
Python3 OS File/Directory Methods
Overview
The os.lseek() method sets the current position of the file descriptor fd to pos, with the method specified by how.
Valid on Unix and Windows.
Syntax
The **lseek()** method syntax is as follows:
os.lseek(fd, pos, how)
Parameters
-
fd -- The file descriptor.
-
pos -- This is the position in the file relative to the given how parameter..
-
how -- The reference position within the file. SEEK_SET or 0 sets pos relative to the start of the file; SEEK_CUR or 1 calculates from the current position; os.SEEK_END or 2 starts from the end of the file.
Return Value
This method has no return value.
Example
The following example demonstrates the use of the lseek() method:
#!/usr/bin/python3
import os, sys
# Open file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Write string
os.write(fd, "This is test")
# All fsync() methods
os.fsync(fd)
# Read string from starting position
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print ("Read String is : ", str)
# Close file
os.close( fd )
print ("关闭文件成功!!")
The output of the above program is:
关闭文件成功!!