Python Data Persistence – Writing to File

Python Data Persistence – Writing to File

The file object needs write permission to be able to save data to a file – which is done by setting the mode parameter to ‘w’.Let us store a famous quote by top computer scientist Alan Kay in the ‘top-quotes.txt’ file.

To begin with, declaring a file object referring to the desired file with ‘w’ mode enabled.

>>> file=open ( ' top-quotes . txt' , ' w' )

The write( ) method sends a string to the file object and stores it in the underlying disk file.

Example

>>> quote=" ' The best way to predict the future is to invent it. ' - Alan Kay"
>>> file .write (quote)
64
>>> file.close ( )

Note that the interactive model shows a number of bytes written. Be sure to close the file and go ahead and view the created file using your favorite text editor software (like Notepad) to confirm that the above quote is stored in it. So, now that we have successfully created a file, let us try to add a few more quotes in it as follows:

Example

>>> file=open (' top-quotes . txt' , ' w')
>>> quote=' ' ' ' There are only two kinds of programming languages: those people
 always bitch about and those nobody uses.' - Bjarne Stroustrup 
'The only way to learn a new programming language is by writing programs in it.' -Dennis Ritchie
 'A computer would deserve to be called intelligent if it could deceive a human into 
believing that it was human.' - Alan Turing' ' '
>>> file. write (quote)
352
>>> file. close ( )

Note the use of triple quotes to form a multi-line string. Open the file again using the editor. To your surprise, the earlier string is not visible now. Why?
The reason is the behavior of ‘w’ mode. It opens a file for writing purposes, erasing its earlier contents if it already exists. Here we want to add few more lines to an existing file. For that, we need to use ‘a’ as a mode parameter to let new data added to an existing file. Following table 5.1 lists other valid mode parameters and their purpose:

Mode parameter

Purpose

R allows the file to be read, (default)
W Opens a file for writing only, erases contents if existing
A appends new data to an existing file.
T stores data in text format (default)
B stores data in binary format.
+ allows simultaneous reading and writing in a file.
x opens the file for exclusive creation.

Going back to our attempt to add new quotes in top-quotes.txt, change the mode parameter to ‘a’.

Example

>>> file=open ( ' top-quotes . txt' , 'a ')
>>> quote=''''There are only two kinds of programming languages: 
those people always bitch about and those nobody uses.' - Bjarne Stroustrup 
'The only way to learn a new programming language is by writing programs in it.' -Dennis Ritchie 
'A computer would deserve to be called intelligent if it could deceive a human into believing that it was human.' 
- Alan Turing'''
>>> file .write (quote).
352
>>> file . close ( )

The file should have earlier text intact and new quotes added after it. File object also possesses writelines ( ) method to write strings in a list object. Each item in the list is treated as one line. Note that the method doesn’t insert ‘\n’ by itself, hence it must be explicitly provided as a part of each string.

Example

>>> file=open ( ' top-quotes . txt' , ' a ' )
>>> quotes= [
'programming languages have a devious influence. They shape our thinking habits - Edsger W. Dijkstra\n',
'programmers do programming not because they expect to get paid or get adulation by the public, 
but because it is fun to program - Linus Torvalds\n',
'A computer would deserve to be called intelligent if it could deceive a human into believing that it was human 
- Alan Turing\n']
>>> file .writelines (quotes) 
>>> file . close ()