Reading and writing of text files

--Originally published at Hector Martinez Alcantara

To write or to read something from a text file, first you have to open a file.

This is the sintaxis of how to open a file:

file object = open("file_name_with_extension_and_directory","type_of_open")

Here is a list of the different modes of opening a file −

Modes Description
r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+ Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for
.
ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

To write something in an opened file, you use the method write, this is the sintaxis:

fileObject.write(string);

To read  something in an opened file, you use the method read, this is the sintaxis:

fileObject.read([count]);

After all, you have to close the file, to close it, you have to do this:

fileObject.close();

Let’s do an example of writing:

file= open("text.txt","w")
file.write("Im a string in a textfile");
file.close()

As you can see, firstly, you open the file, with the w, that means write, then I use the write function to write a string into the textfile and then I close it.

Let’s do an example of reading:

file= open("text.txt","r")
var = file.read(50);
print(var) #The result is the text contained in the textfile until 50 bytes
file.close()

As you can see, firstly, you open the file, with the r, that means read, then I use the read function to read a the text inside the textfile and then I close it.

Thanks for reading, and as usual, special thanks to Tutorialspoint who always have the answers.