Juzoi Mwahhh - You Can Do It!
- File Handling in Python
Difference Between Text File and Binary File
Text File:
- Contains human-readable characters.
- Can be opened with simple editors like Notepad.
- Data is stored in ASCII or Unicode format.
- Example: .txt, .csv
Binary File:
- Contains non-human-readable characters.
- Stores data in the same format as in computer memory.
- Used for images, videos, compiled programs, etc.
- Example: .exe, .jpg, .dat
File Functions in Python
1. open():
- Syntax: open(filename, mode)
- Modes: 'r' (read), 'w' (write), 'a' (append), 'rb', 'wb', etc.
2. read():
- Reads contents of a file.
- Syntax: file.read(size)
3. write():
- Writes data to the file.
- Syntax: file.write(string)
4. seek():
- Moves the cursor to a specific position.
- Syntax: file.seek(offset, whence)
Juzoi Mwahhh - You Can Do It! - File Handling in Python
5. close():
- Closes the file.
- Syntax: file.close()
15 MCQs (With Answers and Explanations)
1. Which mode is used to open a file for reading in binary format?
a) 'rb' b) 'wb' c) 'r' d) 'w'
Answer: a) 'rb'
Explanation: 'rb' stands for read binary.
2. What will file.write() do if the file already contains data and is opened in 'w' mode?
a) Appends data b) Deletes existing data c) Reads data d) None
Answer: b) Deletes existing data
Explanation: 'w' mode overwrites the file.
3. What does file.seek(0) do?
a) Moves cursor to the end b) Moves cursor to start c) Closes file d) None
Answer: b) Moves cursor to start
Explanation: seek(0) resets the cursor to the beginning.
4. How to open a file for appending text?
a) 'wt' b) 'at' c) 'rt' d) 'bt'
Answer: b) 'at'
Explanation: 'a' is append mode.
Juzoi Mwahhh - You Can Do It! - File Handling in Python
5. Which of these is not a valid mode in open()?
a) 'rw' b) 'rb' c) 'wb' d) 'ab'
Answer: a) 'rw'
Explanation: 'rw' is invalid; use 'r+' instead.
6. If you forget to close a file, what might happen?
a) Data loss b) Memory leak c) File remains locked d) All of the above
Answer: d) All of the above
Explanation: Closing file is necessary to free resources.
7. Which function is used to read a single line from a file?
a) read() b) readline() c) readlines() d) readfile()
Answer: b) readline()
Explanation: readline() reads one line at a time.
8. What will file.read() return when the end of the file is reached?
a) Zero b) None c) Empty string d) Error
Answer: c) Empty string
Explanation: read() returns '' when EOF is reached.
9. What is the default mode of open()?
a) 'r' b) 'w' c) 'a' d) 'rb'
Answer: a) 'r'
Explanation: Default is read mode.
Juzoi Mwahhh - You Can Do It! - File Handling in Python
10. What is a file object in Python?
a) A pointer to the file b) A variable storing data c) None d) Function
Answer: a) A pointer to the file
Explanation: File object handles file operations.
11. What will file.readlines() return?
a) Single line b) List of lines c) Characters d) None
Answer: b) List of lines
Explanation: It returns a list of all lines.
12. How to read 10 characters from a file?
a) file.read(10) b) file.readline(10) c) Both a & b d) None
Answer: a) file.read(10)
Explanation: read(n) reads n characters.
13. Which function is used to check the current file cursor position?
a) tell() b) seek() c) read() d) write()
Answer: a) tell()
Explanation: tell() returns cursor position.
14. What does 'ab' mode do?
a) Append text b) Append binary data c) Write text d) Write binary data
Answer: b) Append binary data
15. Which is correct syntax to close a file?
a) close(file) b) file.close() c) shutdown(file) d) file.end()
Juzoi Mwahhh - You Can Do It! - File Handling in Python
Answer: b) file.close()
Explanation: file.close() is correct.
5 Most Important Code-based PYQs
1. Write a Python program to read a text file and display its contents.
2. Write a Python program to copy contents of one file to another.
3. Write a program to count the number of words in a text file.
4. Write a program to read a file and print lines starting with 'A'.
5. Write a program to create a binary file and store roll number and marks of 5 students.
Output Prediction Questions
1.
f = open("sample.txt", "w")
f.write("Hello\nWorld")
f.close()
f = open("sample.txt", "r")
print(f.read())
# Output: Hello
# World
2.
f = open("data.txt", "w")
f.write("Python")
Juzoi Mwahhh - You Can Do It! - File Handling in Python
f.seek(0)
f.write("J")
f.close()
f = open("data.txt", "r")
print(f.read())
# Output: Jython
3.
f = open("test.txt", "w")
f.write("12345")
f.close()
f = open("test.txt", "r")
print(f.read(3))
# Output: 123