Computer Science
If the file 'poemBTH.txt' contains the following poem (by Paramhans Yoganand) :
God made the Earth;
Man made confining countries
And their fancy-frozen boundaries.
But with unfound boundLess Love
I behold the borderLand of my India
Expanding into the World.
HaiL, mother of religions, Lotus, scenic beauty, and sages!
Then what outputs will be produced by both the code fragments given in question1.
Python
Python File Handling
4 Likes
Answer
(a)
my_file = open('poemBTH.txt', 'r')
my_file.read()
God made the Earth;
Man made confining countries
And their fancy-frozen boundaries.
But with unfound boundLess Love
I behold the borderLand of my India
Expanding into the World.
HaiL, mother of religions, Lotus, scenic beauty, and sages!
Working
This code reads the entire content of the file poemBTH.txt
into a single string. Since no specific number of characters is specified, it will read until the end of the file (EOF) is reached.
(b)
my_file = open('poemBTH.txt', 'r')
my_file.read(100)
God made the Earth;
Man made confining countries
And their fancy-frozen boundaries.
But with unfound
Working
This code reads the first 100 characters from the file "poemBTH.txt" into a string. It is important to note that the newline at the end of each line will also be counted as a character.
Answered By
2 Likes
Related Questions
Differentiate between the following :
(i) f = open('diary.txt', 'r')
(ii) f = open('diary.txt', 'w')
How are following codes different from one another ?
(a)
my_file = open('poem.txt', 'r') my_file.read()
(b)
my_file = open('poem.txt', 'r') my_file.read(100)
Consider the file poemBTH.txt given above (in previous question). What output will be produced by following code fragment ?
obj1 = open("poemBTH.txt", "r") s1 = obj1.readline() s2.readline(10) s3 = obj1.read(15) print(s3) print(obj1.readline()) obj1.close()
Write code to open file contacts.txt with shown information and print it in following form :
Name: <name> Phone: <phone number>