We will learn about the Python open() function, that is, we will see how to open a text or binary file.
In the previous lesson we talked about files and saw the difference between binary and text (or ASCII) files. In our examples we will mainly use text files for fixed and variable length archives.
Therefore, to operate on a Python language file we must perform these simple steps:
- First open the file specifying: name, extension (mandatory) and type of access (if in writing, reading, in append mode to add content to an existing file, in reading and writing. Optional data as it has a default value which is in reading). In addition to these values there are other arguments that can be indicated and that we will see gradually in the tutorial;
- then do the operations on the file;
- finally close the file.
- If the file is not in the same folder as the Python file, the correct path must be specified. If you are a beginner I recommend that you leave the file in the same folder as the Python file.
Streams are described through classes, which are a list of properties and methods that are linked together. Once you have declared an object type variable you have all the properties and methods attached to it.
Streams are described through classes, which are a list of properties and methods that are linked together. After declaring a variable of type object, all properties and methods are associated.
One of the functions I want to introduce to you is simply Python open () function, which allows us to open a file.
Let’s see it in detail, assuming we want to open a file called coding.txt which is in the same folder as our Python file.
f = open(‘coding.txt’, ‘r’) # Open in input
Opens the file for character reading only. But be careful, if the file doesn’t exist it will give us an error!
However, we point out that the complete syntax would be this:
f = open(‘coding.txt’, mode = ‘r’)
But we can also omit mode parameter.
Note that we have declared a variable f of type object on which we will then apply the various methods.
Now let’s take another simple example:
f = open(‘coding.txt’, ‘w’) # Open in output
You open the file and allow writing to it. But be careful, if the file exists it is deleted and recreated!
Let’s see in detail the various ways of opening a file.
How to open a file in Python
Let’s deepen the open method in Python by studying in detail the various access modes.
Below is an explanatory table of the types of access to a file in Python.
|r||open(‘coding.txt’,’r’)||Default value. Create the file and open it as read-only. If the file exists it does not exist an error message is returned.|
|w||open(‘coding.txt’,’w’)||Create the file and open it for write only. If the file exists it is deleted.|
|a||open (‘coding.txt’, ‘a’)||Open the file in append mode, that is, it allows you to add text to an already existing file. If the file does not exist, create it.|
|+||open(‘coding.txt’,’r+’)||Open the file in read mode and write mode in append mode. Returns an error if the file does not exist.|
|x||open(‘coding.txt’,’x’)||Create the file, returns an error if the file already exists.|
|t||open(‘coding.txt’,’t’)||Open a file in text mode.|
|b||open(‘coding.jpg’,’b’)||Open a file in binary mode. For example for images.|
Let’s now take an example by combining more options than those listed in the table above:
f = open(‘coding.jpg’, ‘r + b’)
This very simple line of code allows you to open a binary file named coding.jpg for reading and writing.
In this lesson we have therefore introduced the Python open() function in the next we will talk about the write() method.