Opening and Closing Files - PYTHON MADE SIMPLE (2015)

PYTHON MADE SIMPLE (2015)

Chapter 10: Opening and Closing Files

Since the start of this chapter, we have been dealing with basics of trying to read and write inputs in python. This time, I would like us to explore the use of writing and reading real files outside python. Yes, python is a very practical program and one can basically be able to do anything as long as he knows what exactly needs to be done in Python.

Python has simple functions and methods that enable one to be able to manipulate files. This is achieved by the use of the file objects in python.

But before, we get to it:

It is first important to first describe some few other functions such as import. How do you bring in the file from a different directory to Python? This is how it is achieved in Python by using the import statement:

Import statement.

Any python source file can be used a module through the execution of the import statement from some other source file in python.

The import function contains the following module.

The function import has to be placed at the top of the script.

The module would them be loaded only once however, despite the number of times the import functions is used in the script. This allows the program to prevent multiple imports of the file to be executed in the program

You will note that when importing a file, for instance, a XML file converted as a CSV file, first, it has to be imported before any execution code can be defined. This will define what the program will be able to do on the file imported into python.

Python has in built basic functions and methods necessary to manipulate files by default. The file object can be used to do the execution and manipulation of the files you need.

The open Function

A file has to first be imported before it can be read and write in Python. Even before you are able to read and write a file in Python, you will need to first open it up to be read. This is achieved using the inbuilt open () function in Python. This function allows the creation of an object as a file which will then enable the execution of other support functions associated with the file.

The open function comes as below:

Here are parameter details:

· file_name: is used as string that contains the file name of the file that is to be opened in python.

· access_mode: Determines the mode by which the file is required to be opened. It allows for reading, writing and appending among other modes available in Python as will be demonstrated in the table below. However, it always recommended applying the default access mode as read (r).

· Buffering: This a value which can either be zero or any other number, often 1 is used. When the buffer value is fixed as zero (0). No buffering is applied for the files being read. However, when the buffer value is used as (1), buffering is applied at the time the file is being accessed. Always, as long as the buffer value is an integer and a value more than one, the buffering process is initiated in accordance with the suggested buffer size. But if the buffer value is negative, the buffer size adopts a default characteristic.

Modes of opening file is as described in the table below:

Modes

Description

r

A file is opened but it can only be read. The reading starts from the start of the file. This is used as a default mode setting.

rb

This allows the opening of the file but for reading only in the binary mode. The reading starts from the start of the file. This is used as a default mode setting.

r+

This mode allows the opening of a file for reading and writing at the same time. The reading starts from the start of the file. This is used as a default mode setting.

rb+

This mode allows the opening of a file for reading and writing at the same time but in the binary mode. The reading starts from the start of the file. This is used as a default mode setting.

w

Allows a file to be opened but can only be written. It allows the existing file to be overwritten. However, a new file will be created when none of the file described exists.

wb

Allows a file to be opened but can only be written in the binary format. It allows the existing file to be overwritten. However, a new file will be created when none of the file described exists

w+

Allows a file to be opened and cab be read and written at the same time. It allows the existing file to be overwritten. However, a new file will be created when none of the file described exists

wb+

Allows a file to be opened and cab be read and written in the binary format. It allows the existing file to be overwritten. However, a new file will be created when none of the file described exists

a

This allows a file to be opened and can only allow appending of new information. Hence, the file pointer will be at the end of the file if it exists and it is present in the append mode. A new file will also be created if the file mentioned does not exist.

ab

This allows a file to be opened and can only allow appending of new information only in the binary format. Hence, the file pointer will be at the end of the file if it exists and it is present in the append mode. A new file will also be created if the file mentioned does not exist.

a+

It executes the opening of a file and allows appending and reading of the file. The file pointer is at the end of the file if the file exists. Hence, the file pointer will be at the end of the file if it exists and it is present in append and read mode. A new file will also be created if the file mentioned does not exist.

ab+

It executes the opening of a file and allows appending and reading of the file in the binary format. The file pointer is at the end of the file if the file exists. Hence, the file pointer will be at the end of the file if it exists and it is present in append and read mode in binary format. A new file will also be created if the file mentioned does not exist.

How to use the file Object Attributes

Immediately a file has been opened, then, it becomes a file object for which much other information can be derived in python relating to the information in the file.

The following attributes can be executed to the file object:

Attribute

Description

file.closed

This allows Python to returns true when the file is closed, otherwise, it is considered as false.

file.mode

This returns the mode of access for which the file was opened.

file.name

It returns the file name opened in Python.

file.softspace

This will returns false when space is required, otherwise, it will be true.

An example of the program to be executed includes

Example

The output will be as seen in this screenshot:

In most cases, when working with a real/actual file such as an excel file. Then, the import function can be used in this case. Assuming you have a file saved as a CSV file name “goodmen.csv”. You can open it and use it in many other ways. Moreover, you can also check out the name, confirmed if the file is closed or not, confirm the mode and softspace flag as illustrated above. The following program can be used to access it:

Use any other file with a diff name and see what happens with the file. Just change the name goodmen.csv with your new file name in the csv format.

Closing of a file using the close() attribute in Python

This allows Python to close a file object and assumes any information that had not been written and closes the file object. This ensures that the program does not execute any other function after that had been done.

However, python is also able to close a file automatically when a different object is assigned to the file. But, it is a good practice to always close the file using the close () function.

This is implemented using the following syntax:

fileObject.close();

For example:

The previous file opened was f2.

The file can therefore be closed using the attribute:

The output after running this file will be as shown in the screen shot below:

Summary

It is important to know that before you are able to read a file in Python. It must be opened first, using the open function as described in this chapter.

Next, you will be able to execute and find out the attributes of the file using the various attributes as mode, filename, space and many more attributes.

Finally, always ensure that the program is closed using the file_name object.close() function in Python. It allows you to run another program and makes the python process quick in its execution of programs even in limited memory capacity.