0% found this document useful (0 votes)
4 views3 pages

File Handling in Python

The document provides an overview of file handling in Python, detailing how to read, write, and manipulate files using built-in functions. It explains different file modes, types of files, and various file operations including opening, writing, reading, appending, and deleting files. Additionally, it highlights the use of the 'with' statement for automatic file closure and demonstrates handling binary files.

Uploaded by

124421amritraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

File Handling in Python

The document provides an overview of file handling in Python, detailing how to read, write, and manipulate files using built-in functions. It explains different file modes, types of files, and various file operations including opening, writing, reading, appending, and deleting files. Additionally, it highlights the use of the 'with' statement for automatic file closure and demonstrates handling binary files.

Uploaded by

124421amritraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction

 File handling in Python allows us to read, write, and manipulate files (text or
binary) stored on our system.
 Python provides several built-in functions to work with files such as open(),
remove(), write(), etc.

Definition

 A file in Python is a storage unit on a computer where data can be stored


permanently.

File Modes in Python

Mode : Description
“r” : Read (default) – Opens file for reading; error if file doesn’t exist.
“w” : Write – Creates a file if it doesn‘t exist; overwrites if it exists.
“a” : Append – Opens file for writing; does not overwrite existing content.
“x” : Exclusive creation – Creates a new file; fails if file exists.
“t” : Text mode (default) – Handles text files.
“b” : Binary mode – Handles binary files (images, videos, etc.).
Types of Files in Python

Two types –

Text Files (.txt, .csv, .log)

o These files store readable characters (letters, numbers, symbols).


o Example: “Hello, World!” in a .txt file.

Binary Files (.jpg, .pdf, .exe)

o These files store non-readable data (machine code).


o Example: Images, videos, executables.

File Operations

🔹 Opening a File
Syntax:
file = open(“[Link]”, “mode”)
where, “[Link]” → The name of the file.
“mode” → Specifies how the file is opened (read, write, append, etc.).
🔹 Writing to a File

Syntax:
file = open(“[Link]”, “w”) # Open file in write mode
[Link](“Hello, this is a file handling example in Python.”)
[Link]() # Always close the file
NB: If the file already exists, its content is overwritten.
🔹 Reading a File

file = open(“[Link]”, “r”) # Open file in read mode


content = [Link]() # Read entire file
print(content)
[Link]()
Other Read Methods
[Link](10) # Read first 10 characters
[Link]() # Read one line at a time
[Link]() # Read all lines as a list
🔹 Appending to a File

file = open(“[Link]”, “a”) # Open file in append mode


[Link](“\nThis is a new line added!”) # Append data
[Link]()
NB : It does not overwrite existing content; it just appends at the end.
🔹 ‘with’ Statement in File

 Using the ‘with’ statement, the file is automatically closed.

with open(“[Link]”, “r”) as file:


content = [Link]()
print(content) # File closes automatically after the block
🔹 Working with Binary Files
 This is applied for images, PDFs, etc.

with open(“[Link]”, “rb”) as file: # Read binary mode


data = [Link]()
with open(“[Link]”, “wb”) as file: # Write binary mode
[Link](data)
🔹 Deleting a File

 The ‘os’ module is used to delete a file.

import os
if [Link](“[Link]”):
[Link](“[Link]”) # Deletes the file
else:
print(“File does not exist”)

You might also like