File Handling Using Python
File Handling Using Python
File handling is an important activity in every web app. The types of activities that you can
perform on the opened file are controlled by Access Modes. These describe how the file will
be used after it has been opened.
These modes also specify where the file handle should be located within the file. Similar to a
pointer, a file handle indicates where data should be read or put into the file.
1. Read Only ('r’): This mode opens the text files for reading only. The start of the file
is where the handle is located. It raises the I/O error if the file does not exist. This is
the default mode for opening files as well.
2. Read and Write ('r+’): This method opens the file for both reading and writing. The
start of the file is where the handle is located. If the file does not exist, an I/O error
gets raised.
3. Write Only ('w’): This mode opens the file for writing only. The data in existing files
are modified and overwritten. The start of the file is where the handle is located. If the
file does not already exist in the folder, a new one gets created.
4. Write and Read ('w+’): This mode opens the file for both reading and writing. The
text is overwritten and deleted from an existing file. The start of the file is where the
handle is located.
5. Append Only ('a’): This mode allows the file to be opened for writing. If the file
doesn't yet exist, a new one gets created. The handle is set at the end of the file. The
newly written data will be added at the end, following the previously written data.
6. Append and Read (‘a+’): Using this method, you can read and write in the file. If
the file doesn't already exist, one gets created. The handle is set at the end of the file.
The newly written text will be added at the end, following the previously written data.
Below is the code required to create, write to, and read text files using the Python file
handling methods or access modes.
How to Create Files in Python
In Python, you use the open() function with one of the following options – "x" or "w" – to
create a new file:
"x" – Create: this command will create a new file if and only if there is no file
already in existence with that name or else it will return an error.
We've now created a new empty text file! But if you retry the code above – for example, if you try to
create a new file with the same name as you used above (if you want to reuse the filename above)
you will get an error notifying you that the file already exists. It'll look like the image below:
"w" – Write: this command will create a new text file whether or not there is a file in
the memory with the new specified name. It does not return an error if it finds an
existing file with the same name – instead it will overwrite the existing file.
This function inserts the string into the text file on a single line.
Based on the file we have created above, the below line of code will insert the string into the
created text file, which is "myfile.txt.”
This function inserts multiple strings at the same time. A list of string elements is created, and
each string is then added to the text file.
Using the previously created file above, the below line of code will insert the string into the
created text file, which is "myfile.txt.”
How to Read From a Text File in Python
There are three methods of reading data from a text file in Python. They are:
This function returns the bytes read as a string. If no n is specified, it then reads the entire
file.
This function reads a line from a file and returns it as a string. It reads at most n bytes for the
specified n. But even if n is greater than the length of the line, it does not read more than one
line.
This function reads all of the lines and returns them as string elements in a list, one for each
line.
You can read the first two lines by calling readline() twice, reading the first two lines of
the file:
How to Close a Text File in Python
It is good practice to always close the file when you are done with it.
This function closes the text file when you are done modifying it:
The close() function at the end of the code tells Python that well, I am done with this section
of either creating or reading – it is just like saying End.
Example:
The program below shows more examples of ways to read and write data in a text file. Each
line of code has comments to help you understand what's going on:
This is the output of the above code when run in the shell. I assigned "This is Lagos", "This
is Python", and "This is Fcc" to "L" and then asked it to print using the ''file.read'' function.
The code above shows that the "readline()" function is returning the letter based on the
number specified to it, while the "readlines()" function is returning every string assigned to
"L" including the \n. That is, the "readlines()" function will print out all data in the file.