Computer Science
Write a method in Python to read the content from a text file diary.txt line by line and display the same on screen.
Python File Handling
5 Likes
Answer
def diary_content(f):
myfile = open(f, "r")
str = " "
while str:
str = myfile.readline()
print(str, end = '')
myfile.close()
diary_content("diary.txt")
Answered By
4 Likes
Related Questions
Consider the file "contacts.csv" created in above Q. and figure out what the following code is trying to do?
name = input("Enter name :") file = open("contacts.csv", "r") for line in file: if name in line: print(line)
Consider the file poemBTH.txt and predict the output of following code fragment. What exactly is the following code fragment doing ?
f = open("poemBTH.txt", "r") nl = 0 for line in f: nl += 1 print(nl)
Write a method in Python to write multiple line of text contents into a text file mylife.txt.line.
What will be the output of the following code ?
import pickle ID = {1:"Ziva", 2:"53050", 3:"IT", 4:"38", 5:"Dunzo"} fin = open("Emp.pkl","wb") pickle.dump(ID, fin) fin.close() fout = open("Emp.pkl",'rb') ID = pickle.load(fout) print(ID[5])