File I/O in Python - Write operations

File output in Python:

Writing to files is well supported in Python.
The most clear interface to open a file, is through the built-in function open().

Broadly, three types of I/O are available in Python. They are,

  1. Text I/O
  2. Binary I/O
  3. Raw I/O 

In this chapter we will go through, how to write to a file under these different I/O types.

1. Text I/O: Writing text to a file in Python

(a) Writing one line at a time:

FileObject = open('./OutputFile.txt','w')
FileObject.write('Testing the file object, write method')

This example will create a file named OutputFile.txt under the current folder 
from which the program is being run, and write the text 'Testing the file object, write method' 
inside that file.

(b) Writing multiple lines at one go:

MultipleLinesofText = ('Sun\n','Earth\n','Moon\n')
FileObject.writelines(MultipleLinesofText)

The writelines() method takes any sequence such as a tuple as a parameter which contains
multiple lines of text. All the lines from the sequence are written to the file by writelines()
method.

2. Binary I/O: Writing binary contents to a file in Python 

BinaryFileObject = open('./OutputFile.txt','rb+')
Quote2Write = b'The primes have tantalized mathematicians since the Greeks'

BinaryFileObject.write(Quote2Write)
BinaryFileObject.seek(4)
BinaryFileObject.write(b'P')

In the above example after writing a quote, the file pointer is reset to the 5th position
from the beginning of the file and the byte is overwritten with the uppercase version of
the same letter.

Opening a file in binary mode provides lot of flexibility and power in writing the 
file to the disk. The conveniences include the random access.The file pointer can be set to any 
position in the file, which is a powerful feature for any indexing application including 
database systems and applications.

However, it is also possible that if care is not taken manual file manipulation using offset will
result in data integrity issues.
Remember the prefix 'b' to the string Quote2Write. This is to denote a byte literal.
In binary mode writing strings will raise an Exception of type TypeError.


3. Raw I/O : Writing raw contents to a file in Python

OK. What is raw I/O? 
If I/O is handled in binary mode, without any buffering - giving the finest
control over the I/O stream to the programmer - that is Raw I/O in Python.
A buffer is nothing but a collection of bytes.
Though buffering makes it fast by reading blocks of data from hard disk into big chunks,
raw mode gives the control to the programmer. 

With raw I/O in python a programmer can always implement a custom 
buffering mechanism where responsibilities like how a buffer is filled,
how much should be the buffer size, how many buffers should operate from behind ,
different buffer sizes and threads for write and read buffers and many other
design startegies.

Example for Raw I/O in Python:

IndexFileObject = open('index.dat', 'wb+', buffering=0)
IndexFileObject.write(b'No buffers...program to disk...straight...')

The above snippet will create an index.dat file and write the byte literal into that. 


Copyright 2024 © pythontic.com