Opening and closing a file







Hello Folks...!!!

Today let us look at how to handle files in Python.

Opening a file

Python provides a built-in function open( ) for file operations. It provides facilities to open the file in various modes. It will return a file object. The syntax is given below.

file_object=open( 'file_name with path' , 'mode' , buffer_size)

Where, 
  • file_object - A user defined variable name to act as file object
  • file_name - the file to be opened along with its path.
  • mode - Read/write mode or choose one from various existing modes.
  • buffer_size - to denote buffering options while reading/writing.

File open modes

  • r : This is the default mode. It denotes Read only mode. The pointer is placed in the file beginning.

  • rb : This can be used for reading a file in binary format.

  • r+ : For both reading writing the file.

  • w : Write only mode. If the given file does not exist in the specified location, it creates a new file automatically. If a file in the specified name exists, the contents will be over written.

  • wb : Opens the file and it can be written only and that also in binary format. This also creates a new file in the specified location if it doesn't exist.

  • wb+ : The opened file can be Read and Written in binary format.

  • a : It denote appending. If the specified file does not exist in the specified location, it creates a new file and writes to it. If the specified file already exists, the file pointer is placed at the end and the writing of contents begin from there.

  • a+ : Opens a file for both appending and reading.

  • ab+ : The file is opened and it can be read and appended in binary format.

Once a file is opened a file object associated with it is created. We can get the information about the file using those objects.

  • file_object.closed : Returns a boolean value. If the file is closed returns true else false.

  • file_object.mode : Returns in which mode the file was opened.

  • file_object.name : Returns the name of the opened file.

Buffer_size : The buffer is a middle layer where the content will be stored before writing into file. It is optional parameter. If you specify a value of 0 for it, it indicates no buffer. If you specify 1 it means we are opting for line buffer. Line buffer is nothing but the contents will be written to the buffer until a new line is encountered. Once a new line is encountered the contents will be transferred to the.specified file. Values greater than 1 denotes we are customizing the buffer size. When the buffer is full the contents will be written into the opened file.

Closing a file

If you open a file using open(), then you have to close it explicitly using the built-in method close(). The syntax is file_object.close(). It requires no parameters to be passed. It just closes the file opened by the file_object and after executing this, no operations can be performed on the closed file.

For example :


If you simply copy paste the path of the file, you will get this error. The '\' is an escape character. So we have to overcome this. Before that we can open and close the files using try-except-finally clauses. Look at the code below.




Now the except block will catch the error and display a customized message. Now let us look at how to clear this error. When you need to use escape characters as normal ones, then it should be preceded by a backward slash. So we have to write a backward slash in front of all the '\' in the file path. Look at the code below.




Now it works fine....!!!!!!. I have opened the file in read mode.

Tomorrow let us look at some file opening and closing hacks in Python.....


Next Topic👉

Comments