0% found this document useful (0 votes)
2 views11 pages

Python File Handling

Uploaded by

mani952133
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)
2 views11 pages

Python File Handling

Uploaded by

mani952133
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

Python File Handling

Dilini Chamika Silva


1

Overview
File handling in Python allows you to create, read, write, and modify files stored on disk.
Python provides built-in functions and modules that make file operations simple and
efficient.

1. What is File Handling?


File handling refers to working with files, such as:
●​ Creating files
●​ Opening files
●​ Reading data from files
●​ Writing data to files
●​ Appending data
●​ Closing files
Python mainly uses the built-in open() function for file handling

2. Opening a File

Syntax
2

Common File Modes

Mode Description

r Read (default) – error if file does not exist

w Write – creates new file or overwrites existing file

a Append – adds data to the end of the file

x Create – error if file already exists

rb Read binary

wb Write binary

r+ Read and write

3. Closing a File
Always close a file after use to free system resources.

4. Reading Files

4.1 Read Entire File


3

4.2 Read a Specific Number of Characters

4.3 Read Line by Line

4.4 Read All Lines as a List

4.5 Using a Loop


4

5. Writing to Files

5.1 Write Mode (w)

6. Appending to Files

Append Mode (a)


Adds content to the end of the file.

7. Creating Files

Create Mode (x)


5

8. Using with Statement (Recommended)


The with statement automatically closes the file.

Writing with with:

9. File Methods

Method Description

read() Reads entire file

readline() Reads one line

readlines() Reads all lines into a list

write() Writes a string to file

writelines() Writes list of strings

seek() Changes file cursor position

tell() Returns current cursor position


6

Example:

10. Handling Binary Files


Used for images, audio, videos, etc.

Writing binary data:

11. File Handling with Exception Handling


7

12. Deleting Files


Using the os module:

14. Text File Handling (.txt)


Text files store data in human-readable form.

Writing to a Text File

Reading from a Text File


8

15. JSON File Handling (.json)


JSON (JavaScript Object Notation) is used to store structured data.

Import JSON Module

Writing JSON to a File

Reading JSON from a File

Convert JSON String


9

16. CSV File Handling (.csv)


CSV (Comma Separated Values) files store tabular data.

Import CSV Module

Writing to a CSV File

Reading from a CSV File


10

Using DictReader and DictWriter

17. Comparison Summary

File Type Module Used Data Format

Text Built-in Plain text

JSON json Key–Value (Structured)

CSV csv Tabular

Thank You!

You might also like