Computer Science
When a file is opened for output in write mode, what happens when
(i) the mentioned file does not exist
(ii) the mentioned file does exist ?
Python File Handling
4 Likes
Answer
When a file is opened for output in write mode ("w" mode) in Python, the behaviour differs depending on whether the mentioned file already exists or not:
(i) If the mentioned file does not exist — If the file specified in the open() function does not exist, Python will create a new file with the given name. The file will be opened for writing, and any data written to it will be written from the beginning of the file.
(ii) If the mentioned file does exist — If the file specified in the open() function already exists, Python will truncate existing data and over-write in the file. It's essential to be cautious when opening existing files in write mode, as any existing data will be lost when the file is opened in "w" mode.
Answered By
2 Likes
Related Questions
Which of the following Python modules is imported to store and retrieve objects using the process of serialization and deserialization ?
- csv
- binary
- math
- pickle
Which of the following function is used with the csv module in Python to read the contents of a csv file into an object ?
- readrow()
- readrows()
- reader()
- load()
What role is played by file modes in file operations ? Describe the various file mode constants and their meanings.
What are the advantages of saving data in :
(i) binary form
(ii) text form
(iii) csv files ?