Computer Science
Differentiate between the following :
(i) f = open('diary.txt', 'r')
(ii) f = open('diary.txt', 'w')
Python File Handling
2 Likes
Answer
(i) f = open('diary.txt', 'r')
— This line opens the file diary.txt
in read mode ('r'). If the file does not exist, Python will raise an error. If the file exists, the data will not be erased.
(ii) f = open('diary.txt', 'w')
— This line opens the file diary.txt
in write mode ('w'). If the file does not exist, Python creates new file with the specified name. If the file exists, Python will truncate existing data and over-write in the file.
Answered By
1 Like
Related Questions
If you rename a text file's extension as .csv, will it become a csv file ? Why/why not ?
Differentiate between "w" and "r" file modes used in Python while opening a data file. Illustrate the difference using suitable examples.
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)
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.