In projects, you are often required to read data from a file or write data to a file. Python provides inbuilt functions for handling read/write operations on files. In this tutorial, you'll learn how to read/write a file. Let's get started.
Opening a file
Before you read or write a file, you'll need to open a file first. The in-built method open() is used to open a file. It will create a file object which will provide you methods to read or write a file.
Let's look at the syntax of open() method.
file_object = open( r'file_name', 'access_mode')
As you can see here, it has 2 parameters:
- file_name — the name of the file that you want to access.
- access_mode — specifies the type of access...