Python 3 – os.lseek() Method
When working with files in Python, you often need to change the current position in the file. Python provides the os
module that contains various methods to perform file operations, including changing the current position in the file. os.lseek()
is one of these methods that allows you to set the current position of the file pointer.
Syntax
The syntax to use the os.lseek()
method in Python is as follows:
os.lseek(fd, pos, how)
Where:
fd
: It is the file descriptor of the file to whom the position must be set. File descriptor is a unique identifier that identifies an open file in a process.pos
: It is an offset from the point specified byhow
argument.how
: It is a constant that specifies the reference point from which the offset is counted.
The how
argument can take one of the following values:
os.SEEK_SET
: Sets the current position from the beginning of the file.os.SEEK_CUR
: Sets the current position from the current position of the file.os.SEEK_END
: Sets the current position from the end of the file.
Return Value
The os.lseek()
method returns the new current position of the file pointer. If the call fails, it returns -1
.
Example
Here’s an example to demonstrate the usage of os.lseek()
method:
import os
# Open the file in read-write mode
fd = os.open("example.txt", os.O_RDWR)
# Read the first 5 characters from the file
data = os.read(fd, 5)
print("Read data:", data.decode())
# Move the file pointer 3 positions ahead from the current position
pos = os.lseek(fd, 3, os.SEEK_CUR)
print("New position:", pos)
# Read the next 3 characters from the file
data = os.read(fd, 3)
print("Read data:", data.decode())
# Move the file pointer 2 positions back from the current position
pos = os.lseek(fd, -2, os.SEEK_CUR)
print("New position:", pos)
# Read the next 2 characters from the file
data = os.read(fd, 2)
print("Read data:", data.decode())
# Close the file
os.close(fd)
Output:
Read data: Hello
New position: 8
Read data: W
New position: 6
Read data: or
In this example, we first open a file “example.txt” in read-write mode using os.open()
with a file descriptor fd
. We then read the first 5 characters from the file using os.read()
method, and print the read data.
We then move the file pointer 3 positions ahead from the current position using os.lseek()
, and get the new position using the return value of the method. We then read the next 3 characters from the file using os.read()
method, and print the read data.
We again move the file pointer 2 positions back from the current position using os.lseek()
, and get the new position. We then read the next 2 characters from the file using os.read()
method, and print the read data.
Finally, we close the file using os.close()
method.
Conclusion
In this article, we learned about the Python os.lseek()
method that allows you to set the current position of the file pointer. We also saw an example to demonstrate the usage of this method. os.lseek()
method is useful when you need to change the current position in the file while reading or writing data.