File Handling Test 6
Q1. Do we need to use the close() function when we use 'with' statement to open a file?
Q2. Which mode you will choose for the following situations:
1. For reading file
2. For creating new file if not existing
3. All previous data over written by new data.
4. All new data added at the end of the file
Q3. Write the code to open a file "try.txt" in write mode by using 'with' statement.
Q4. When we open a file in append mode. Write T/F for the following statements
- New file created, if file does not exist.
- Error comes, if file does not exist.
- File open with no data, if file already exist and had some data.
- New data will over wrie the existing data.
- New data will add at the end of the existing data.
SOLUTIONS
Q1. Do we need to use the close() function when we use 'with' statement to open a file?
Ans. No
Q2. Which mode you will choose for the following situations:
1. For reading file
2. For creating new file if not existing
3. All previous data over written by new data.
4. All new data added at the end of the file
Ans.
1. Read mode
2. Write mode/Append mode
3. Write mode
4. Append mode
Q3. Write the code to open a file "try.txt" in write mode by using 'with' statement.
Ans. with open("try.txt" , 'w') as f1
Q4. When we open a file in append mode. Write T/F for the following statements
- New file created, if file does not exist.
- Error comes, if file does not exist.
- File open with no data, if file already exist and had some data.
- New data will over wrie the existing data.
- New data will add at the end of the existing data.
- True
- False
- False
- False
- True