Python Programming – File Handling

In this Page, We are Providing Python Programming – File Handling. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – File Handling

File Handling

There are several ways to present the output of a program; data can be printed on computer monitor in a human-readable form, or written to a file (for example, image.jpg, notes.txt, etc.) for future use. A computer file (or simply “file”) is a resource for storing information, which is available to a computer program, and is usually based on some kind of durable electronic storage. A file is durable in the sense that it remains available for programs to use after the current program has finished. Computer file can be considered as the modern counterpart of paper document which traditionally are kept in office files, library files, etc., and this is the source of the term.

File opening

A common operation needed during program execution is to load data from an existing file or to create a new file to store data. To accomplish this, the program first needs to open a file, which is executed using open () function. The open () returns a file object, and is most commonly used with two arguments, filename and mode.

The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used, mode can be ‘ r ‘ when the file will only be read, ‘ w’ for only writing (an existing file with the same name will be erased), and ‘ a ‘ opens the file for appending, any data written to the file is automatically added to the end. The mode argument is optional,with ‘r’ as default. Modes ‘r+’, ‘w+’ and ‘a+’ opens the file for both for updating (note that ‘w+’ truncates the file).

Windows operating system makes a distinction between text and binary files, so in Windows, ‘b’ appended to the mode opens the file in binary mode, therefore, there are also modes like ‘ rb\ ‘wb’, and ‘r+b’.

To understand the various methods which are helpful in reading information from the file, a text file is manually created (having empty line after every text line) having filename list.txt, and kept at path C:/test. The content of the file is:

This is first line.

This is second-line.

This is third line.

This is fourth and last line.

Now, open the file using open ( ) function in read mode.

>>> f=open ( ' C : / test / list . txt ' , ' r ' )

Writing to a file

Apart from only reading information from a file, there can be a scenario where some data need to be written to a file. To carry out such operation, write (string) method is used, where string argument should be of string data type. Upon success, write ( ) method returns None. Open “list.txt” and add some text at the end of the file.

>>> f=open ( ' C : / test / list . txt ' , ' a+ ' ) 
>>> f . write ( ' \ n \ n This is the new last line . ' )
>>> f . seek ( 0 , 0 )
>>> f . read ( )
' This is first line . \ n \ n This is second line . \ n \ n This is third line . \ n \ n This is fourth and last line . \ n \ n This is the new last line . ' 
>>> f . close ( )

File renaming and deletion

Python’s os module provide methods for renaming and deleting files. To rename a file, use rename (src, dst) method, where src argument is source ilename, while dst is destination filename.

>>> import os
>>> os . rename ( ' C : / test / list . txt ' , ' C : / test / newlist . txt ' )

To delete a file, use remove ( ) method.

>>> os . remove ( ' C : / test / newlist . txt ' )