Python Data Persistence – File I0

Python Data Persistence – File I0

‘File’ is an indispensable word in the vocabulary of even an ordinary computer (even mobile) user. Every day, he is required to deal with files which may be documents, spreadsheets, presentations, images, and so on. Slightly advanced users, to whom we may call developer, prepare scripts, build executables which are also files.

When a user starts an application, he enters certain data, either through a keyboard or any other device such as a mouse, camera, scanner, and so on. The data goes into the computer’s main memory and is further processed as per the process defined in the application. If this data – input or resulting from the process – is needed for subsequent use, it is saved in a computer file, because if left in the computer memory, it will be erased when a computer is turned off. In this chapter, we shall discuss how data from Python program is stored in persistent disk files.

A Python console application interacts with peripheral devices through its built-in input ( ) and print ( ) functions. Channels of interaction between processor and peripheral devices are called streams. A stream is an object that sends/receives a continuous flow of data. Python’s input ( ) function reads data from standard input streaming device i.e. keyboard that is recognized as sys. stdin object defined in sys built-in the module. Similarly, the print ( ) function sends data to a standard output device which is a computer display screen (monitor), defined as sys. stdout object.

The stdin object has read ( ) and readline ( ) methods to accept user input through the keyboard. The read ( ) method accepts data till the stream is terminated by Ctrl+D character. On the other hand readline ( ) method accepts all keystrokes till the ‘Enter’ key is pressed. Both methods leave ‘\n’ at the end of the input.

Example

>>> data=sys.stdin.read( )
Hello
How are you?
>>> data
'Hello\nHow are you?\n'
>>> data=sys.stdin.readline()
Hello How are you?
>>> data
'Hello How are you?\n'
>>>

In fact, the input ( ) function performs stdin. readline ( ) and returns by stripping the trailing ‘\n! character. The write ( ) method available to the stdout object does exactly what the print ( ) function does. It sends the argument data to the default output device – the computer monitor. However, when using interactive mode, it also displays the size of the object in bytes.

Example

>>> sys.stdout.write(data)
Hello, How are you?
19
>>>

Any object that can send/receive the stream of bytes is called ‘File like object in Python. A file (like) object can invoke read ( ) or write ( ) methods depending upon the stream to which it is connected. Hence, stdin and stdout objects are file-like objects too. Python can perform 10 operations with objects representing disk files, network sockets, memory arrays, and so on. In this chapter, we shall deal with computer disk files. These files store data in a persistent manner, which is often the need as the same collection of data may be needed repeatedly. Instead of laboriously keying in the same data again and again from the keyboard, reading it from a file becomes more efficient and less error-prone. Similarly, screen output being temporary and limited, can instead be stored in files, (figure 5.1)

Python Data Presistence - File 10 chapter 5 img 1

Standard 10 streams communicating with stdin and stdout objects are always available. To use a disk file for reading/writing purposes file object needs to be declared first, by using the built-in open () function.