PYTHON
FILE HANDLING
PYTHON FILE
HANDLING
File handling in Python refers to the process
of creating, opening, reading, writing, and
closing files using Python’s built-in functions
and methods.
It allows programs to store data permanently
on disk and retrieve it later, unlike variables
that lose data when the program ends.
2
OPEN()
• The key function for working with files in Python is the open() function.
• The open() function takes two parameters; filename, and mode.
• There are four different methods (modes) for opening a file:
• “r” - Read - Default value. Opens a file for reading, error if the file does not exist.
• "a" - Append - Opens a file for appending, creates the file if it does not exist.
• "w" - Write - Opens a file for writing, creates the file if it does not exist.
• "x" - Create - Creates the specified file, returns an error if the file exists.
3
OPEN()
• In addition you can specify if the file should be handled as binary or text mode.
• "t" - Text - Default value. Text mode.
• "b" - Binary - Binary mode (e.g. images)
4
SYNTAX
• To open a file for reading it is enough to specify the name of the file:
• The code above is the same as:
• Because "r" for read, and "t" for text are the default values, you do not need to specify
them.
5
OPEN A FILE ON THE SERVER
Assume we have the following file, located in the same folder as Python:
• To open the file, use the built-in open() function.
• The open() function returns a file object, which has a read() method for reading the content
of the file:
6
OPEN A FILE ON THE SERVER
If the file is located in a different location, you will have to specify the file path,
like this:
7
USING THE WITH STATEMENT
You can also use the with statement when opening a file:
Then you do not have to worry about closing your files, the with statement takes
care of that.
8
CLOSE FILES
• It is a good practice to always close the file when you are done with it.
• If you are not using the with statement, you must write a close statement in
order to close the file:
9
READ ONLY PARTS OF THE FILE
• By default the read() method returns the whole text, but you can also specify
how many characters you want to return:
10
PYTHON FILE WRITE
WRITE TO AN EXISTING FILE
• To write to an existing file, you must add a parameter to the open() function:
• "a" - Append - will append to the end of the file
• "w" - Write - will overwrite any existing content
11
PYTHON FILE WRITE
OVERWRITE EXISTING CONTENT
• To overwrite the existing content to the file, use the w parameter:
12
PYTHON FILE WRITE
CREATE A NEW FILE
• To create a new file in Python, use the open() method, with one of the following
parameters:
• "x" - Create - will create a file, returns an error if the file exists
• "a" - Append - will create a file if the specified file does not exists.
• "w" - Write - will create a file if the specified file does not exists.
13
PYTHON FILE WRITE
CREATE A NEW FILE
• To create a new file in Python, use the open() method, with one of the following
parameters:
• "x" - Create - will create a file, returns an error if the file exists
• "a" - Append - will create a file if the specified file does not exists.
• "w" - Write - will create a file if the specified file does not exists.
14
PYTHON DELETE FILE
DELETE A FILE
• To delete a file, you must import the OS module, and run its [Link]()
function:
• To avoid getting an error, you might want to check if the file exists before you try
to delete it:
15
PYTHON DELETE FILE
DELETE FOLDER
• To delete an entire folder, use the [Link]() method:
16
PYTHON FILE HANDLING
INDIVIDUAL ACTIVITY
INDIVIDUAL ACTIVITY
Task:
Create a python program that:
[Link] student names into a file
[Link] and displays them
[Link] one new student
[Link] updated list
Submission instructions:
• Submit on time (This day): March 27, 2026 @ 11:59PM
• Attach the file on the posted Google Classroom Activity and submit
• File to submit must be a .py
18
THANK YOU
Dhen Marc Ginos
[Link]@[Link]