Reading and writing of text files

--Originally published at Quirino´s Projects

southpark

The most useful thing about doing console programs in python is the ability to read and write files, it gives us access to create and read files within our computer, without the need to copy/paste into our code and having a way to store the results of the program.

To get a file in our code first we have to open()

We define the name of the variable the file will be stored into and we open it

myFile = open(‘robots.txt’,’r+’)

The sintaxis is

open

(

Name of the file as a string

,

Parameter

)

To check more parameters about file I/O check this table from tutorialspoint.com

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.
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 writing.
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.

The code above opens a file in a way that this can be read and written. To read something to the file we do myFile.read() which returns the contents of the file within a string and if we pass an integer as a parameter to .read() it returns the number of bits to read, and to write we do myFile.write(string) we write the string into the file

When we are done using the file we simply close it

myFile.close()