- Introduction
- File Handling Basics
- File Operations
- File Modes
- Reading and Writing Files
- Directories
- CSV Files
- JSON Files
- Scenario-Based Questions
- Comparison Questions
- Quick Revision
- Final Notes
This document contains the most frequently asked interview questions related to Python File Handling.
It is useful for:
- Freshers
- Python Developers
- Technical Interviews
- Placement Preparation
- Revision Before Interviews
File Handling is the process of creating, opening, reading, writing, updating, and deleting files using a programming language.
File Handling allows programs to store data permanently instead of keeping it only in memory.
- Text Files
- Binary Files
RAM stores temporary data that is lost when the program ends, whereas file storage preserves data permanently.
- .txt
- .csv
- .json
- .xml
- .html
- .md
- .jpg
- .png
- .docx
- .xlsx
- .mp3
- .mp4
- Create
- Open
- Read
- Write
- Append
- Update
- Close
- Delete
Closing files releases system resources and ensures that buffered data is written properly.
The open() function opens a file and returns a file object that can be used for reading, writing, or updating the file.
open(file_name, mode)r(Read Mode)
- File Name (or File Path)
- File Mode
A file mode specifies how a file should be opened and what operations are allowed.
Opens an existing file for reading.
Opens a file for writing.
If the file already exists, its contents are overwritten.
Appends data to the end of the file.
Creates a new file.
If the file already exists,
Python raises
FileExistsErrorAllows both reading and writing.
Allows reading and writing.
Existing contents are overwritten.
Allows appending and reading.
- rb
- wb
- ab
Reads the entire file or a specified number of characters.
Reads one line at a time.
A list containing all lines in the file.
Writes a string to a file.
Writes multiple strings from an iterable to a file.
It does not automatically add newline characters.
It automatically closes the file after the block finishes, even if an exception occurs.
FileNotFoundErrorFileExistsErrorA directory is a folder used to organize files and other directories.
ospathlibos.getcwd()os.chdir()os.mkdir()os.makedirs()os.rmdir()os.rename()os.listdir()os.path.exists(path)or
Path(path).exists()Comma-Separated Values.
csvEach row as a list.
Each row as a dictionary using column names as keys.
Writes rows to a CSV file.
Writes dictionary data to a CSV file.
JavaScript Object Notation.
jsonWrites a Python object to a JSON file.
Converts a Python object into a JSON string.
Reads JSON data from a file.
Converts a JSON string into a Python object.
Serialization is the process of converting a Python object into JSON format.
Deserialization is the process of converting JSON data back into Python objects.
| read() | readline() |
|---|---|
| Reads entire file or specified characters | Reads one line |
Write (w) |
Append (a) |
|---|---|
| Overwrites existing data | Preserves existing data and adds new data |
| CSV | JSON |
|---|---|
| Tabular data | Key-value structured data |
| Rows and columns | Objects and arrays |
| Best for spreadsheets | Best for APIs |
| dump() | dumps() |
|---|---|
| Writes to a file | Returns a JSON string |
| load() | loads() |
|---|---|
| Reads from a file | Reads from a string |
axJSON.
CSV.
osor
pathlibThe with statement.
Remember:
open()→ Opens filer→ Readw→ Writea→ Appendx→ Createread()→ Entire filereadline()→ One linereadlines()→ List of lineswrite()→ Write stringwritelines()→ Write multiple stringswith→ Automatic closeos→ Directory operationspathlib→ Object-oriented paths- CSV → Tabular data
- JSON → Structured data
- Serialization → Python → JSON
- Deserialization → JSON → Python
After completing this chapter, you should be able to:
- Work confidently with Python File Handling.
- Choose the correct file mode for different tasks.
- Read and write text, CSV, and JSON files.
- Perform directory operations.
- Understand serialization and deserialization.
- Answer common File Handling interview questions with confidence.