ADVERTISEMENT
ADVERTISEMENT

Python program to read character till a count

Here, we are going to learn how to read character till a count in Python?
Submitted by Shivang Yadav, on March 16, 2021

File Handling in Python is reading and writing data from files. Programs can interact with files and data using the concept of file handling.

To read characters from file till a specific character count from the current pointer position, we will use read() method and pass the count as parameter.

To get back to the starting position, we will use seek() method.

Program to read data from file till a specific position

# Python program to read character till a count

# main function
def main():
    # opening the file in read mode.
    file = open("data.txt","r")

    # printing file name 
    print("Name of the File : ",file.name)

    # reading 10 characers 
    String = file.read(10)
    print("Read String is till 10       : ",String)
    position = file.tell()
    print("Current Position             : ",position)
    String =file.read(10)
    print("Read String is till next 10  : ",String)
    position = file.tell()
    print("Current Position             : ",position)
    print()
    print("Sending Pointer back to Top")
    position = file.seek(0,0)
    print("Current Position             : ",position)
    String =file.read(25)
    print("Read String is till 25       : ",String)
    position = file.tell()
    print("Current Position             : ",position)

    file.close()

if __name__=="__main__":main()

File : File.dat

learn python programming language at inclduehelp.com

Output:

Name of the File :  data.txt
Read String is till 10       :  learn pyth
Current Position             :  10
Read String is till next 10  :  on program
Current Position             :  20

Sending Pointer back to Top
Current Position             :  0
Read String is till 25       :  learn python programming 
Current Position             :  25

Python file handling programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.