Python Programming – File Management in Python
In all the programs that we have developed so far using Python language, we see that the output is lost when we exit from the program. However, in certain situations, it is necessary to save the data for future use. This can be achieved by making use of files. A file is a place on the disk with a certain name to store related information. This information is stored on the secondary storage memory known as a hard disk, flash drive, etc. However, if we do not store the data on the hard disk in the form of a file, then it resides in the computer’s primary memory RAM (Random Access Memory). The RAM is a volatile memory that loses its data when the computer is turned off. However, a hard disk is permanent storage and also called non¬volatile memory.
A computer programmer or data entry operator always prefers to enter the data in the files instead of storing the data in temporary storage. This is done for the following reasons:
- The complete data will be lost, if either the system is turned off or power failure occurs.
- Repetition of the same data entry will be a time-consuming process.
- It is cumbersome to enter a huge amount of data through the keyboard or any other input device.
- Data cannot be corrected or updated.
- The data is unreliable if it needs to enter through the keyboard.
- In case of an error during the data entry process, the entire process will be started all over again.
- Problems can also be faced during testing and debugging during the program.
- Validation and verification of data is not possible without saving it on the file.
- The reliability of information increases while saving it on the disc as a file.
- The efficiency of the program also increases.
Therefore, to overcome all the issues listed above files provide a better approach for the permanent storage of important and processed information. More precisely, a file is defined as “A file is a single entity pertains to a collection of related data structure.”
- Python Programming – File Processing
- Python Programming – Working With Binary Files
- How to Pickle Unpickle Tutorial | Understanding Python Pickling & Unpickling with Example
Python File Methods
Python provides various methods associated with files to work with. These methods are used in conjunction with file objects. The list of file methods is given in Table 9.4. Some of these methods have been illustrated in the previous sections.
|close()||Closes an open file. It has no effect if the file is already closed.|
|detach()||Separates the underlying binary buffer from the TextIOBase and return it.|
|fileno()||Returns an integer number (file descriptor) of the file.|
|flush()||Flushes the write buffer of the file stream.|
|isatty()||Returns True if the file stream is interactive.|
|read(q)||Readsatmost n characters form the file. Reads till end of file if it is negative or None.|
|readable()||Returns True if the file stream can be read from.|
|readline(n=-l)||Reads and return one line from the file. Reads in at most n bytes if specified.|
|readlines(n=-l)||Reads and return a list of lines from the file. Reads in at most nbytes/characters if specified.|
|seek(offset,from=SEEK_SET)||Changes the file position to offsetbytes, in reference to from (start, current, end).|
|seekable()||Returns True if the file stream supports random access.|
|tell()||Returns the current file location.|
|truncate(size=None)||Resizes the file stream to size bytes. If size is not specified, resize to current location.|
|writable()||Returns True if the file stream can be written to.|
|write(s)||Writes string s to the file and return the number of characters written.|
|writelines(lines)||Writes a list of lines to the file.|
|Note: All these methods are used with the fileobject. E.g. fp.close( ), where fp is the file object.|
tell( ) and seek( ) Methods
tell(): This method is used to determine the current position of a file pointer. It can be used either while writing to a file or reading from the file. The syntax of telling is given as follows:
seek(): This method is used to move the file pointer to a particular location in the file. The syntax for seek( ) is given in as follows:
|seek(offset [, from])|
In the above syntax, the offset represents the number of bytes to be moved from the specified position. The from argument represents the reference Programming in Python position from where the bytes are to be relocated. The value of from can be one of the values given in Table 9.5.
The programming illustration of tell() and seek() methods is given in Code 9.12. In this program, we see that tell(), tells the current position of the file, and seek() is reading the file from the current position.
Code: 9.12, Illustration of tell( ) and seek ( ) methods.
|# Illustration of tell() and seek() methods
with open(“data.txt”,’r’,encoding = ‘cpl252’) as fp:
In this chapter, we have discussed data files in detail. Various methods of file and directory handling in Python are discussed with the programming implication of each. The file creation, opening, and closing are discussed with various modes of operation such as reading and writing to a file. Apart from that file renaming, deleting, making a directory, changing the directory, and removing directory methods are discussed.