- Python Basics
-
Overview
- Introduction to Python and Installation
- Variables and Data Types
- Conditional Statements (if-else)
- Loops (for, while)
- Functions and Lambda Expressions
- Lists, Tuples, and Dictionaries
- File Handling (Reading/Writing Files)
- Exception Handling (Try, Except)
- Modules and Packages
- List Comprehensions and Generators
File Handling (Reading/Writing Files)
Add to Bookmark1. Introduction
File handling in Python allows reading, writing, and managing files efficiently. Python provides built-in functions for working with text and binary files.
2. Opening a File
Python uses the open() function to handle files.
file = open("example.txt", "r") # Opens a file in read mode
file.close() # Always close the file after useFile Modes
| Mode | Description |
|---|---|
"r" | Read (default), file must exist |
"w" | Write, creates a new file or overwrites existing content |
"a" | Append, adds data to an existing file |
"x" | Create, fails if file already exists |
"rb" | Read binary mode |
"wb" | Write binary mode |
3. Reading a File
Reading Entire Content
with open("example.txt", "r") as file:
content = file.read()
print(content)
with open()ensures the file is closed automatically.
Reading Line by Line
with open("example.txt", "r") as file:
for line in file:
print(line.strip()) # Removes newline charactersReading Specific Number of Characters
with open("example.txt", "r") as file:
print(file.read(10)) # Reads first 10 charactersReading Lines into a List
with open("example.txt", "r") as file:
lines = file.readlines() # Reads all lines into a list
print(lines)4. Writing to a File
Writing and Overwriting a File
with open("example.txt", "w") as file:
file.write("Hello, World!\n")
file.write("Python File Handling Tutorial")
"w"mode overwrites existing content.
Appending to a File
with open("example.txt", "a") as file:
file.write("\nAppending new content!")5. Working with Binary Files
Binary files store data like images, videos, and executables.
Reading a Binary File
with open("image.png", "rb") as file:
data = file.read()
print(data[:10]) # Display first 10 bytesWriting to a Binary File
with open("copy.png", "wb") as file:
file.write(data)6. Checking if a File Exists
import os
if os.path.exists("example.txt"):
print("File exists!")
else:
print("File not found!")7. Summary
Use open("filename", "mode") to work with files.
Always close files or use with open() for automatic closing. "r" to read, "w" to write, "a" to append, and "x" to create new files.
Handle text and binary files separately.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- Python Challenging Programming Exercises Part 3
- Time Series Analysis on Air Passenger Data
- Quantum AI – The Future of AI Powered by Quantum Computing
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- Best Platform to Learn Digital Marketing in Free
- AI Agents: The Future of Automation, Work, and Opportunities in 2025
- AI & Space Exploration – AI’s Role in Deep Space Missions and Planetary Research
- Loan Default Prediction Project Using Machine Learning
- 10 Awesome Data Science Blogs To Check Out
- Navigating AI Careers in 2025: Data Science, Machine Learning, Deep Learning, and More
- How to Start Your Career as a DevOps Engineer
- Understanding SQL vs MySQL vs PostgreSQL vs MS SQL vs Oracle and Other Popular Databases
- Extract RGB Color From a Image Using CV2
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset


