ADVERTISEMENT
ADVERTISEMENT

Read a program from another file in Python

Here, we are going to learn how to read a program from another file in Python?
Submitted by Shivang Yadav, on February 12, 2021

Problem Statement: Python program to read a program from another file.

Problem Description: We need to read the contents from a python file in a program and print its content.

We will use the concepts of file handling in python and read and print a program from a file .

Program to illustrate the program

Main.py:

F=open("HirarchicalInheritance.py","rb")

data=F.read(20)
print(data.decode())
print("==================")

F.seek(40,1) # 1 current position ..move pointer to 60th position
data=F.read()
print(data.decode())

F.close()

HirarchicalInheritance.py:

F=open("HirarchicalInheritance.py","rb")

data=F.read()
print(data.decode())

F.seek(10,0) # 0 begin of file..Move Pointer to 10th Position
data=F.read()
print(data.decode())

F.seek(-40,2) # 2 end of file
data=F.read()
print(data.decode())

Output:

F=open("HirarchicalInheritance.py","rb")

data=F.read(20)
print(data.decode())
print("==================")

F.seek(40,1) # 1 current position ..move pointer to 60th position
data=F.read()
print(data.decode())

F.close()

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.