Topics

File Handling

Welcome to a tutorial on File Handling in Python. In a developer's life, cases may arise, where data from a stored raw file needs to be obtained, as such you need to connect those files with a program that it will read, write or modify the data. Firstly, we will look at the different file access modes.

 

File Access Modes

However, there exist various purposes for using a file along with Python, that is to read, write, append, etc., is important to specify each time what operations Python should expect on the file. in addition, several access modes can be used to specify while opening a file in a program.

Python programming language can manage two types of files, that is:

  • Text or normal characters
  • Binary or Text only (i.e containing 1 and 0). 

Also, along with the access mode, it is important to specify which type of file it will open.

 

Python - Opening a File

Now, for use to connect our program with the desired file, there is a need for a stream that can fetch or write data in it. Therefore, there are already some in-built classes/functions which we can utilize to solve this purpose. Check out how to do this in the example below:

>>> myFile = open([path of file], [access mode], [buffer size])

 

From the above, myFile will be the object of the file, and method open() will open the file specified in the [file path]; also, the other two arguments, [access mode] will provide the access mode and [buffer size] will ask for how many chunks of data needs to be retrieved from the file. However, the last two parameters i.e. [access mode] and [buffer size] are completely optional or by choice. In addition, by default, when there is no access mode specified, then it is considered to be reading mode. 

The [path of file] can be the complete path of the file or if it exists in the same file as a program, as such only providing the name is sufficient. Check below for clarity.

From the case above:

>>> myFile = open("file.txt")

The above is sufficient. But, in situations like this:

It is important to know that the file.txt is not in the same folder as in the above example, as such, you need to specify the whole location.

>>> myFile = open("C:/c_code/file.txt")

 

So, when the file had been opened we have something called a pointer that points to some position in the file. Thus this pointer is used to read or write from that position. This pointer is can be said to be quite similar to a text cursor that could be shifted freely (i.e. by using arrow keys) or used to write or delete the text from a text, however, the pointer can shift/read/write through some functions only.

The access modes that are to be specified in the argument are outlined below:

1.read mode: The "r" is for text files, and "rb" is for binary files. The file pointer points at the beginning of the file.

>>> myFile = open("file.txt", "r")

 

2. write mode: The "w" is for text files, and "wb" is for binary files. The file pointer points at the beginning of the file.

>>> myFile = open("file.txt", "w")

 

3. append mode: The "a" is for text files and "ab" for binary files. The file pointer points at the end of the file.

>>> myFile = open("file.txt", "a")

 

4. read/write mode: The "r+" or "w+" provides the option for doing read and write operations on the same file object, and "rb+" or "wb+" for binary files. The file pointer points at the beginning of the file.

>>> myFile = open("file.txt", "r+")

 

5. append/read - The "a+" is to enable read/append mode, and "ab+" is for append/read mode on binary files. The file pointer points at the end of the file.

>>> myFile = open("file.txt", "a+")

From the above example, opening files that way is quite similar to creating an object a file and doing some operations with/on it for reading/writing. Also, opening files, in the same way, create a stream of the data buffer, thus, after using the file it is always recommended to close this stream. 

In addition, the above can be done by using the close function. Check the example below:

>>> myFile.close()

Also, you can use the file by making use of the keyword. Check the syntax below to understand how it is used.

with open("file.txt", "r+"):
		// operations to perform on "file.txt"
		// there can be many
		// syntax is pretty much like that of methods

From the example, we are creating a scope for the file to be used. Also, if the program gets out of this scope the file gets closed itself.