Python | os.lseek() method - GeeksforGeeks

22 Nov, 2019
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
os.lseek() method sets the current position of file descriptor fd to the given position pos which is modified by how.
Syntax: os.lseek(fd, pos, how)

Parameters: 
fd:  This is the file descriptor on which seek is to be performed.
pos:  This is the position in the file with respect to given parameter how.
It can accept three values which are 

    os.SEEK_SET or 0 to set the position relative to the beginning of the file
     os.SEEK_CUR or 1 to set the position relative to the current position
    os.SEEK_END or 2 to set the position relative to the end of the file.

how: This is the reference point in the file. It also accepts three values which are

    os.SEEK_SET or 0 to set the reference point to the beginning of the file
          os.SEEK_CUR or 1 to set the reference point to the current position
    os.SEEK_END or 2 to set the reference point to the end of the file.


Return Value:  This method does not returns any value.


Example #1 :

Using os.lseek() method to seek the file from beginning


                
                    
                        
                            
                                
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                
                            
                        
                    
                    # Python program to explain os.lseek() method          # importing os module  import os      # path  path = 'C:/Users/Rajnish/Desktop/testfile.txt'  # Open the file and get # the file descriptor associated # with it using os.open() method fd = os.open(path, os.O_RDWR|os.O_CREAT)   # String to be written s = 'GeeksforGeeks - A Computer Science portal'  # Convert the string to bytes  line = str.encode(s)   # Write the bytestring to the file  # associated with the file  # descriptor fd  os.write(fd, line)   # Seek the file from beginning # using os.lseek() method os.lseek(fd, 0, 0)   # Read the file s = os.read(fd, 13)   # Print string print(s)   # Close the file descriptor  os.close(fd) 
                        
                    
                
                
                    
                        
                        Output:b'GeeksforGeeks'
Example #2 :Using os.lseek() method to to seek the file from specific position# Python program to explain os.lseek() method         # importing os module import os     # path path =  'C:/Users/Rajnish/Desktop/testfile.txt'  # Open the file and get# the file descriptor associated# with it using os.open() methodfd = os.open(path, os.O_RDWR|os.O_CREAT)  # String to be writtens = 'GeeksforGeeks'  # Convert the string to bytes line = str.encode(s)  # Write the bytestring to the file # associated with the file # descriptor fd os.write(fd, line)    # Seek the file after position '2'# using os.lseek() methodos.lseek(fd, 2, 0)  # Read the files = os.read(fd, 11)  # Print stringprint(s)  # Close the file descriptor os.close(fd)Output:b'eksforGeeks'
My Personal Notes
arrow_drop_upSave
os.SEEK_SET or 0 to set the position relative to the beginning of the file
    
 os.SEEK_CUR or 1 to set the position relative to the current position
    
os.SEEK_END or 2 to set the position relative to the end of the file.

os.SEEK_SET or 0 to set the reference point to the beginning of the file
         
 os.SEEK_CUR or 1 to set the reference point to the current position
    
os.SEEK_END or 2 to set the reference point to the end of the file.

# Python program to explain os.lseek() method          # importing os module  import os      # path  path = 'C:/Users/Rajnish/Desktop/testfile.txt'  # Open the file and get # the file descriptor associated # with it using os.open() method fd = os.open(path, os.O_RDWR|os.O_CREAT)   # String to be written s = 'GeeksforGeeks - A Computer Science portal'  # Convert the string to bytes  line = str.encode(s)   # Write the bytestring to the file  # associated with the file  # descriptor fd  os.write(fd, line)   # Seek the file from beginning # using os.lseek() method os.lseek(fd, 0, 0)   # Read the file s = os.read(fd, 13)   # Print string print(s)   # Close the file descriptor  os.close(fd) 
                    
b'GeeksforGeeks'

Example #2 :
Using os.lseek() method to to seek the file from specific position
# Python program to explain os.lseek() method         # importing os module import os     # path path =  'C:/Users/Rajnish/Desktop/testfile.txt'  # Open the file and get# the file descriptor associated# with it using os.open() methodfd = os.open(path, os.O_RDWR|os.O_CREAT)  # String to be writtens = 'GeeksforGeeks'  # Convert the string to bytes line = str.encode(s)  # Write the bytestring to the file # associated with the file # descriptor fd os.write(fd, line)    # Seek the file after position '2'# using os.lseek() methodos.lseek(fd, 2, 0)  # Read the files = os.read(fd, 11)  # Print stringprint(s)  # Close the file descriptor os.close(fd)
b'eksforGeeks'

nidhi_biet
python-os-module
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
Python OOPs Concepts
Python | Get unique values from a list
Check if element exists in list in Python
Python Classes and Objects
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Python | Pandas dataframe.groupby()
Create a directory in Python