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.
Q5. a ) Write a program to add the following data in a file named "data.txt"
Welcome to my Blog
This blog is for python learners
Q6. Write the output, if you execute the following program twice.
f = open("data.txt",'a')
f.write("Welcome to my Blog\n")
f.write("This blog is for python learners")
f.close()
Q7. Write the following code in one statement:
f = open("data.txt",'r')
f.read(10)
Q8. Write a program to read and display three lines(line by line) from file "data.txt"
Q9. Write the output if the file contains the following lines :
Welcome to my Blog
This blog is for python learners
This blog is for python learners
a)
f=open("data.txt", "r")
print(f.readline( ))
print(f.readline( ))
print(f.readline( ))
b)
f=open("data.txt", "r")
print(f.readline( )end=' ')
print(f.readline( )end=' ')
print(f.readline( )end=' ')
Q10. Write a program to read entire file "data.txt" line by line.
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.
Ans.
- True
- False
- False
- False
- True
Q5. a ) Write a program to add the following data in a file named "data.txt"
Welcome to my Blog
This blog is for python learners
Ans. f = open("data.txt",'a')
f.write("Welcome to my Blog\n")
f.write("This blog is for python learners")
f.close()
Q6. Write the output, if you execute the following program twice.
f = open("data.txt",'a')
f.write("Welcome to my Blog\n")
f.write("This blog is for python learners")
f.close()
Ans
Welcome to my Blog
This blog is for python learnersWelcome to my Blog
This blog is for python learners
Q7. Write the following code in one statement:
f = open("data.txt",'r')
f.read(10)
Ans. open("data.txt",'r').read(10)
Q8. Write a program to read and display three lines(line by line) from file "data.txt"
Ans.
f=open("data.txt", "r")
print(f.readline( ))
print(f.readline( ))
print(f.readline( ))
Q9. Write the output if the file contains the following lines :
Welcome to my Blog
This blog is for python learners
This blog is for python learners
a)
f=open("data.txt", "r")
print(f.readline( ))
print(f.readline( ))
print(f.readline( ))
b)
f=open("data.txt", "r")
print(f.readline( )end=' ')
print(f.readline( )end=' ')
print(f.readline( )end=' ')
Ans a)
Welcome to my Blog
This blog is for python learners
This blog is for python learners
b)
Welcome to my Blog
This blog is for python learners
This blog is for python learners
Q10. Write a program to read entire file "data.txt" line by line.
Ans.
f = open("data.txt", "r")
line=f.readlines( )
for i in line:
print(i, end = ' ')
f.close( )
Thorough & Useful: Qollabb's Python course guarantees Python programming proficiency by providing in-depth analysis, coding challenges, and real-world projects.
ReplyDeletepython cs