Python os.lseek() Method

Python os.lseek() Method

  1. Syntax of Python os.lseek() Method
  2. Example 1: Use os.lseek() to Seek the File From the Beginning
  3. Example 2: Use os.lseek() to Seek the File From a Specific Position

Python os.lseek() method is an efficient way of setting the current position of a file descriptor fd to a new position pos, depending on how mode.

Syntax of Python os.lseek() Method

os.lseek(fd, pos, how)

Parameters

fd The file descriptor/path to be processed.
pos It determines the position in the file depending on the following parameters:
1. SEEK_SET/0: It sets the position to the beginning of the file.
2. SEEK_CUR/1: It sets the position relative to the current position in the file.
3. SEEK_END/2: It sets the position relative to the end of the file.
how It is the reference point in the file depending on the following parameters:
1. SEEK_SET/0: It sets the reference point to the beginning of the file.
2. SEEK_CUR/1: It sets the reference point to the current position in the file.
3. SEEK_END/2: It sets the reference point to the end of the file.

Return

In the execution process, this method does not return any value.

Example 1: Use os.lseek() to Seek the File From the Beginning

import os

path_address = './File.txt'

mode_set = 0o666

flags = os.O_CREAT | os.O_RDWR

descriptor = os.open(path_address, flags, mode_set)

print("The new file has been created.")

line = "Hello, World! Programming is fun!"

os.write(descriptor, line.encode())

print("The text has been successfully entered into the file.")

os.lseek(descriptor, 0, 0)

line = os.read(descriptor, os.path.getsize(descriptor))

print("Reading the text from the file:")

print(line.decode())

os.close(descriptor)

print("The file descriptor has been closed successfully.")

Output:

The new file has been created.
The text has been successfully entered into the file.
Reading the text from the file:
Hello, World! Programming is fun!
The file descriptor has been closed successfully.

It’s a healthy practice for any programmer to close all the newly opened files using the os.close() method.

Example 2: Use os.lseek() to Seek the File From a Specific Position

import os

f_d = os.open( "File.txt", os.O_RDWR|os.O_CREAT )

string= "Hello, World! Programming is fun."

os.write(f_d, string.encode())

os.fsync(f_d)

os.lseek(f_d, 14, 0)

str = os.read(f_d, 100)

print ("Reading the string is: ", str)

os.close(f_d)

print ("The file has been closed successfully.")

Output:

Reading the string is:  b'Programming is fun.'
The file has been closed successfully.

Note that some new operating systems can support more values for pos and how parameters, like os.SEEK_HOLE or os.SEEK_DATA.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method