✍️Introduction
File Handling in Python allows us to create, read, write, and update files.
It is very important for storing data permanently.
File handling enables Python programs to interact with files stored on disk, making it possible to save program output and retrieve data later.
Using file handling, large amounts of data can be managed efficiently, which is essential for applications like data logging, configuration storage, and report generation.
Python uses the open() function to work with files.
Why File Handling is Important?
-
Store data permanently
-
Read large data
-
Used in real-world applications
-
Useful in projects and interviews
Open a File in Python:
open() function is used to access a file in Python by specifying its name and the mode of operation.The mode determines whether the file is opened for reading, writing, or appending, and whether the file is treated as text or binary.
Syntax:
File Modes:
| Mode | Description |
|---|---|
| r | Read (default) |
| w | Write (creates new file) |
| a | Append |
| x | Create file |
| rb | Read binary |
| wb | Write binary |
Read mode (
r) opens an existing file to read its contents.-
Write mode (
w) creates a new file or overwrites an existing file. -
Append mode (
a) adds new data to the end of a file without deleting existing data. -
Create mode (
x) creates a file and raises an error if the file already exists. -
Binary modes (
rb,wb) are used to read or write non-text files like images, audio, or videos.
Reading a file in Python means accessing and displaying the contents of an existing file.
Read Line by Line:
The
readline() method reads one line at a time from the file, making it useful for handling files with multiple lines or structured data.Read All Lines:
readlines() method reads all lines of a file at once and returns them as a list, where each line is an element.It is useful when you want to process or display all lines together, but it should be used carefully with very large files.
2️⃣ Write to a File :
Writing to a file in Python means storing new data into a file using write mode.
When a file is opened in write mode (w), Python creates the file if it does not exist.
If the file already exists, its previous content is completely erased before writing new data.
⚠ Note: Write mode overwrites existing data.
3️⃣ Append to a File :
a), Python creates the file if it does not exist.New data is always added at the end of the file, preserving the existing content.
4️⃣ Using with Statement (Best Practice) :
The with statement is used to open files safely and automatically close them after use.
Using with open() is considered best practice because it automatically handles file closing, even if an error occurs.
This makes the code safer, cleaner, and easier to manage.
✔ Automatically closes file
✔ Safer and cleaner
Check if File Exists :
os.path.exists() function is used to check if a file or folder exists.It helps prevent errors when trying to open a file that does not exist, making programs more reliable and safe.
Delete a File :
Deleting a file means removing a file permanently from the system.
In Python, the os.remove() function is used to delete a file from the specified location.
This operation permanently removes the file, so it should be used carefully, usually after checking that the file exists.
Real-Life Example: Save Student Data
This program saves student information into a file for permanent storage.
File handling is commonly used in real-life applications to store records such as student details, marks, and reports.
Using the with statement ensures the file is safely opened and automatically closed after writing.
Common File Functions :
They allow programmers to read content, write new data, move the file cursor, and check the current position, making file handling more flexible and efficient.
| Function | Use |
|---|---|
| read() | Read full file |
| write() | Write data |
| close() | Close file |
| seek() | Change cursor |
| tell() | Get position |
❌ Common Mistakes:
-
Forgetting to close file
-
Using wrong mode
-
Reading non-existing file
File Handling Interview Questions ?
1️⃣ Difference betweenw and aw (write mode) overwrites the existing file content, while a (append mode) adds new data at the end without deleting existing content.2️⃣ What is
with statement?with statement is used to open a file safely and automatically close it after use.Large files can be read efficiently by reading them line by line using
readline() or a loop instead of loading the entire file into memory.
Comments
Post a Comment