Given a text file as an input to the program, the program
should accept the filename and open the file in read mode using the open
function. read the contents of the file and print the contents of the
file on to the console.
let's use all the three functions read(), readlines() and readline()
Write a Python Program to Read The Text File Using read()
Take
a string variable named filename, use the input function call and ask
a user to enter the file name and store the filename into a string
variable.
Once we get the filename, use the try block to open the
file in read mode and store the file descriptor. Using the file
descriptor to to read the contents of the file using read() function
call.
Contents of the file in a variable named data. Print contents of the data.
Use an exception to catch the file not found error. if file is not found print the appropriate message and exit program
filename = input("Enter a Filename to Read : ")
try:
fd_read = open(filename, "r")
data = fd_read.read()
print(data)
fd_read.close()
except FileNotFoundError:
print("File not Found !")
exit(-1)
Write a Python Program to Read The Text File Using readlines()
Ask
the user to enter the filename and store that into a variable. Use the
try block to open the file in the read mode and store the file
descriptor.
Use the for loop to iterate all the lines using
readlines() function call. Same time as we iterate through lines of a
file, use print to display the line on to the console.
Use exceptions to catch file not found error, print appropriate message and exit the program if file is not found.
filename = input("Enter a Filename to Read : ")
try:
fd_read = open(filename, "r")
for line in fd_read.readlines():
print(line)
fd_read.close()
except FileNotFoundError:
print("File not Found !")
exit(-1)
Write a Python Program to Read The Text File Using readline()
Get the filename from the user and open the file in read mode, Store the file descriptor into a variable inside a try block.
Inside
the try block try to read the first line of a file using readfile()
use while loop to check if line is valid / not null. in the while loop
keep printing the line and keep reading the the file line using the
readline()
filename = input("Enter a Filename to Read : ")
try:
fd_read = open(filename, "r")
line = fd_read.readline()
while line:
print(line)
line = fd_read.readline()
fd_read.close()
except FileNotFoundError:
print("File not Found !")
exit(-1)
Write a Python Program to Read The First Character of Text File Using read()
The
above program can be used to read only a single character from a file.
Just use the read(1) to read the single character from the file.
To read the continuous characters from the file use the read function in loop.
filename = input("Enter a Filename to Read : ")
try:
fd_read = open(filename, "r")
data = fd_read.read(1)
while data:
print(data, end="")
data = fd_read.read(1)
fd_read.close()
except FileNotFoundError:
print("File not Found !")
exit(-1)
Input to the program sample.txt file content
===================================
Hello world
How are you ???
Output of the program sample.txt as a input
====================================
Enter a Filename to Read : sample.txt
Hello world
How are you ???
Process finished with exit code 0
Conclusion
============
Try running the program by yourself, comment down below if you have any queries
Can you write a program to read the file without using read readlines and readline.
0 Comments