Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

File Handling in Python

File handling in Python allows for creating, reading, updating, and deleting files using built-in functions like open(), read(), write(), and close(). Common file modes include 'r' for reading, 'w' for writing, 'a' for appending, and 'x' for creating files. Best practices include using the 'with' statement for proper file closure, handling exceptions, and understanding file positions with tell() and seek().

Uploaded by

abhishekji1604
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

File Handling in Python

File handling in Python allows for creating, reading, updating, and deleting files using built-in functions like open(), read(), write(), and close(). Common file modes include 'r' for reading, 'w' for writing, 'a' for appending, and 'x' for creating files. Best practices include using the 'with' statement for proper file closure, handling exceptions, and understanding file positions with tell() and seek().

Uploaded by

abhishekji1604
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

File handling

In Python
Introduction

File handling in Python allows you to create, read, update, and delete files.
Python provides built-in functions and modules to work with files effectively.
The primary functions and methods for file handling include open(),
read(), write(), close(), and more. Let's go through these with
detailed examples.

File Modes in Python

The open() function is used to open a file. It takes two main arguments:

1. Filename - The name of the file.


2. Mode - Specifies the purpose of opening the file.

Common file modes:

● 'r': Read (default mode). Opens the file for reading; error if the file
doesn’t exist.
● 'w': Write. Opens the file for writing, creating it if it doesn’t exist, or
truncating it to overwrite if it does.
● 'a': Append. Opens the file for appending, creating it if it doesn’t
exist.
● 'x': Create. Creates the file but raises an error if it already exists.
● 'b': Binary mode. Used with other modes for binary files (e.g.,
images).
● 't': Text mode (default). Used with other modes for text files.
Example 1: Reading a File

Example 2: Writing to a File

Example 3: Appending to a File

Example 4: Reading Line by Line


Example 5: Reading Specific Number of Characters

Example 6: Using tell() and seek()

● tell(): Returns the current position of the file pointer.


● seek(): Changes the file pointer to a specified position.

Example 7: Handling Binary Files


Example 8: Exception Handling in File Operations

Example 9: Checking if a File Exists

Using the os module:

Best Practices

1. Use with Statement: It ensures proper closing of the file.


2. Error Handling: Always handle exceptions like
FileNotFoundError.
3. Relative vs Absolute Paths: Use appropriate paths for portability.
Additional Considerations:

● Error Handling: Use try-except blocks to handle potential errors


like file not found, permission denied, etc.
● Binary Files: Use binary mode ('rb', 'wb') for working with
non-text files like images or executable programs.
● File Positions: Use file.seek() to move the file pointer to a
specific position.
● File Information: Use file.tell() to get the current position,
file.readline() to read a single line, and file.readlines()
to read all lines into a list.

File handling is a fundamental concept in Python that enables working with


persistent storage. These examples cover common operations, ensuring a
solid understanding of file I/O.

For Better understanding use Google Colab Notebook

You might also like