Python File Handling

 

Python File Handling

Till now, we were taking the input from the console and writing it back to the console to interact with the user.

Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console since the memory is volatile, it is impossible to recover the programmatically generated data again and again.

The file handling plays an important role when the data needs to be stored permanently into the file. A file is a named location on disk to store related information. We can access the stored information (non-volatile) after the program termination.

The file-handling implementation is slightly lengthy or complicated in the other programming language, but it is easier and shorter in Python.

In Python, files are treated in two modes as text or binary. The file may be in the text or binary format, and each line of a file is ended with the special character.

Hence, a file operation can be done in the following order.

  • Open a file
  • Read or write - Performing operation
  • Close the file

Opening a file

Python provides an open() function that accepts two arguments, file name and access mode in which the file is accessed. The function returns a file object which can be used to perform various operations like reading, writing, etc.

The files can be accessed using various modes like read, write, or append. The following are the details about the access mode to open a file.

SN

Access mode

Description

1

r

It opens the file to read-only mode. The file pointer exists at the beginning. The file is by default open in this mode if no access mode is passed.

2

rb

It opens the file to read-only in binary format. The file pointer exists at the beginning of the file.

3

r+

It opens the file to read and write both. The file pointer exists at the beginning of the file.

4

rb+

It opens the file to read and write both in binary format. The file pointer exists at the beginning of the file.

5

w

It opens the file to write only. It overwrites the file if previously exists or creates a new one if no file exists with the same name. The file pointer exists at the beginning of the file.

6

wb

It opens the file to write only in binary format. It overwrites the file if it exists previously or creates a new one if no file exists. The file pointer exists at the beginning of the file.

7

w+

It opens the file to write and read both. It is different from r+ in the sense that it overwrites the previous file if one exists whereas r+ doesn't overwrite the previously written file. It creates a new file if no file exists. The file pointer exists at the beginning of the file.

8

wb+

It opens the file to write and read both in binary format. The file pointer exists at the beginning of the file.

9

a

It opens the file in the append mode. The file pointer exists at the end of the previously written file if exists any. It creates a new file if no file exists with the same name.

10

ab

It opens the file in the append mode in binary format. The pointer exists at the end of the previously written file. It creates a new file in binary format if no file exists with the same name.

11

a+

It opens a file to append and read both. The file pointer remains at the end of the file if a file exists. It creates a new file if no file exists with the same name.

12

ab+

It opens a file to append and read both in binary format. The file pointer remains at the end of the file.

 

The close() method

Once all the operations are done on the file, we must close it through our Python script using the close() method. Any unwritten information gets destroyed once the close() method is called on a file object.

We can perform any operation on the file externally using the file system which is the currently opened in Python; hence it is good practice to close the file once all the operations are done.

Writing the file

To write some text to a file, we need to open the file using the open method with one of the following access modes.

w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file.

a: It will append the existing file. The file pointer is at the end of the file. It creates a new file if no file exists.

The file related methods

The file object provides the following methods to manipulate the files on various operating systems.

SN

Method

Description

1

file.close()

It closes the opened file. The file once closed, it can't be read or write anymore.

2

File.fush()

It flushes the internal buffer.

3

File.fileno()

It returns the file descriptor used by the underlying implementation to request I/O from the OS.

4

File.isatty()

It returns true if the file is connected to a TTY device, otherwise returns false.

5

File.next()

It returns the next line from the file.

6

File.read([size])

It reads the file for the specified size.

7

File.readline([size])

It reads one line from the file and places the file pointer to the beginning of the new line.

8

File.readlines([sizehint])

It returns a list containing all the lines of the file. It reads the file until the EOF occurs using readline() function.

9

File.seek(offset[,from)

It modifies the position of the file pointer to a specified offset with the specified reference.

10

File.tell()

It returns the current position of the file pointer within the file.

11

File.truncate([size])

It truncates the file to the optional specified size.

12

File.write(str)

It writes the specified string to a file

13

File.writelines(seq)

It writes a sequence of the strings to a file.

 


0 Comments