Reading and Writing files

There is a built ing function in Python which is used for opening files. The function is called open() and takes two arguments:

  • the filename (required argument),
  • the opening mode (optional argument).

Opening mode tells Python for what reason to open the file. The possible values for that argument are:

  • "r" - Read mode which is used when the file is only being read, this is the default value of the argument, if it is not explicitly declared,
  • "w" - Write mode which is used to edit and write new information to the file (any existing files with the same name will be erased when this mode is activated),
  • "a" - Appending mode, which is used to add new data to the end of the file; that is new information is automatically amended to the end,
  • "r+" - Special read and write mode, which is used to handle both actions when working with a file

The open() function works closely with the close() function, which tells Python to close the opened file.

Let's, for example, write the lyrics of John Lennong's "Imagine" song to a new text file. Then that file will be used for reading purposes.

Part 1: Writing to txt files


In [1]:
lyrics = "Imagine all the people, \nliving life in peace... \n\tJohn Lennon"
print(lyrics)


Imagine all the people, 
living life in peace... 
	John Lennon

In [2]:
our_file = open("imagine_lyrics.txt","w")

So now a new text file titled "imagine_lyrics" was created for writing purposes (please take a look to the file dimension .txt which was mandatory to include). We can now use the write() function to write different text to our new file. We will write there the lyrics.


In [3]:
our_file.write("Imagine by John Lennon")

In [4]:
our_file.write(lyrics)

As mentioned above, once the actions with the opened file are complete, you may already close it.


In [5]:
our_file.close()

Part 2: Reading from .txt files

Once the file already exists, we can now read it back to Python. Again, we must open it, but in read only mode this time.


In [6]:
lyrics_imported = open("imagine_lyrics.txt","r")

In [7]:
print(lyrics_imported)


<open file 'imagine_lyrics.txt', mode 'r' at 0x00000000040C71E0>

As you can see above, hte file was succesfully read, but printing it gives only some info, and not the content of the file. For that reason, the read only opened file should also be read, using the built-in read() function.


In [8]:
lyrics_imported.read()


Out[8]:
'Imagine by John LennonImagine all the people, \nliving life in peace... \n\tJohn Lennon'

In [10]:
print lyrics_imported.read()



If you give any integer as an argument to read() function, then it will return only that much very first characters of the string (e.g. read(7) would return "Imagine", while read(4) would return "Imag").

One thing you may have already noticed about read() function is that it returns one full string from the text file. Yet, you might also be interested in reading the file line-by-line. For that purpose, the readlines() function can come handy.


In [19]:
lyrics_imported.readlines()


Out[19]:
[]

In Jupyter notebook (this is specific to Jupyter notebook only), the opened read only file can be read only once. So In order to read it again, we have to open it once again.


In [33]:
lyrics_imported = open("imagine_lyrics.txt","r")

In [34]:
lyrics_imported.readlines()


Out[34]:
['Imagine by John LennonImagine all the people, \n',
 'living life in peace... \n',
 '\tJohn Lennon']

As you can see, not only it read each line separately, but the output we received has the type of list. We may check it, if we save the results in a new file.


In [35]:
lyrics_imported = open("imagine_lyrics.txt","r")
lyrics_list = lyrics_imported.readlines()

In [38]:
type(lyrics_list)


Out[38]:
list

Fine, now we are sure that the output is a list, so we can perform different actions on it


In [39]:
lyrics_list[0]


Out[39]:
'Imagine by John LennonImagine all the people, \n'

In [40]:
lyrics_list[1]


Out[40]:
'living life in peace... \n'

Once verything is done, we should remember to close the file down.


In [42]:
lyrics_imported.close()

Final note: besides readlines() function there is only the singular version called readline() which returns a single line from the text file (e.h. readline(3) will return the whole content of the 3rd line only).

Part 3: automatically closing files

One of the main drawbacks of the above approach is that you shouls always remember to close the files once your actions are completed. A good shortcut is to write all content to a new variable, and close your file immediately. For that purpose a very handy statement called with exists, which closes the file itself. Let's try it out.


In [43]:
with open("imagine_lyrics.txt","r") as data_imported:
    data = data_imported.readlines()

As it can be seen above, no close function was used, as the with statement automatically closes the file down. Another important thing to note, is that the data_imported varible was a temporary name used for an imported file to perform actions on. So we wrote its content to a new global variable (called data) that can be used further for analysis.


In [44]:
print data


['Imagine by John LennonImagine all the people, \n', 'living life in peace... \n', '\tJohn Lennon']

In [45]:
print data_imported


<closed file 'imagine_lyrics.txt', mode 'r' at 0x00000000040C7300>