Files are used to store different types of data. The data can be text or ASCII characters or binary or JSON data etc. Python can be opened any type of file and read, write, edit the file contents in different modes like text or binary. In this tutorial, we examine how to open files in different modes like reading (read-only), writeable, or append (edit) mode.
File Open Modes
As stated previously Python provides different modes to open files. Below we list some of the most popular file open modes. These modes are used with the
open() method in order to set file mode.
|r||Open file for reading which is the default mode if not specified.|
|w||Open a file for writing. If the file does not exist creates it and if the existing file is emptied.|
|x||Open a file for exclusive creation. If the file already exists the open operation fails.|
|a||Opens a file for appending which does not change existing data.|
|t||Open a file in text mode which is the default mode.|
|b||Opens a file in binary mode.|
|+||Opens a file for updating which means reading and writing.|
Open File Read-Only
A file can be opened as read only in Python by using the
"r" mode. Read-only opened file can not be edited or appends or delete or overwritten. It can only read wtihıut any change. In the following example we open the file named “data.txt” as read only.
file = open("data.txt","r")
Open File Write Mode
If we want to open a file in order to write some data we should use th write mode which is expressed with the “w” option. If the file exist all content is deleted and opened as write mode. If the file do not exist the file is created and opened as writeable. The file can not be read if opened in “write” mode.
file = open("data.txt","w")
Open File Read and Write Mode
A file can be also opened in both read and write mode. In read and write mode we can both read the file and write some data. The “r+” is used to express the file is opened as read and write mode.
file = open("data.txt","r+")
Open File Append Mode
The append mode is very similar to the write mode but only new data can be appended to the end of the file. If the file do not exist new file is created.
file = open("data.txt","a")
Open File Binary Mode
By default all files are opened in text mode or ASCII mode. In text mode the contents are added, edited and removed as text. The binary mode is used to put deciaml, hexadecimal and similar type of binary data. The “b” mode is used to opned
Open Text File with Specified Encoding
If the file is opened with the text mode the content of the file is interpreted and displayed with specific encoding. By default the “ASCII” is used as encoding format. But we can also specify the encoding format in order to open a file. The
encoding parameter is used to opened file with the specified encoding format. In the following example we open the file with the “UTF8” encoding.
file = open("data.txt","r",encoding="utf-8")
Files are opened to make operations and opening a file take some system resources. In order to complete the operations the changes should be written into the disk properly. So in order to complete the file operations the file should be closed after operations. Also this releases resources taken for file operations. The
close() method is used to close a file. The file descriptor is provided as parameter to the close() method.
file = open("data.txt","w") close(file)