File Handling in Python

python_tutorials

Introduction

It is an unwritten consensus that Python is one of the best starting programming languages to learn as a novice. It is extremely versatile, easy to read/analyze, and quite pleasant to the eye. The Python programming language is highly scalable and is widely considered as one of the best toolboxes to build tools and utilities that you may want to use for diverse reasons.

This article will briefly covers how Python handles one of the most important components of any operating system: its files and directories. Fortunately, Python has built-in functions to create and manipulate files, either flat files or text files. The io module is the default module for accessing files, therefore we will not need to import any external library for general IO operations.

The key functions used for file handling in Python are: open(), close(), read(), write() and append().

Opening Files with open()

This function returns a file object called “handle”, which is used to read from and write to a file. The arguments that the function can receive are as follows:

open(filename, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Normally, only the filename and mode parameters are needed, while the others are used implicitly set with their default values.

The following code snippet shows how this function can be used:

file_example = open ("TestingText.txt") 

This will open a text file called “TestingText” in read-only mode. Take note that only the filename parameter was specified, this is due to the “read” mode being the default mode for the open function.

The access modes available for the open() function are as follows:

  • r: Opens the file in read-only mode. Starts reading from the beginning of the file and is the default mode for the open() function.
  • rb: Opens the file as read-only in binary format and starts reading from the beginning of the file. While binary format can be used for different purposes, it is usually used when dealing with things like images, videos, etc.
  • r+: Opens a file for reading and writing, placing the pointer at the beginning of the file.
  • w: Opens in write-only mode. The pointer is placed at the beginning of the file and this will overwrite any existing file with the same name. It will create a new file if one with the same name doesn’t exist.
  • wb: Opens a write-only file in binary mode.
  • w+: Opens a file for writing and reading.
  • wb+: Opens a file for writing and reading in binary mode.
  • a: Opens a file for appending new information to it. The pointer is placed at the end of the file. A new file is created if one with the same name doesn’t exist.
  • ab: Opens a file for appending in binary mode.
  • a+: Opens a file for both appending and reading.
  • ab+: Opens a file for both appending and reading in binary mode.

If both the Python file being executed and the target file to read doesn’t exist in the same directory, we need to pass the full path of the file to read, to the open() function as shown in the following code snippet:

file_example = open ("F:DirectoryAnotherDirectorytestsTestingText.txt")

Note: Something to keep in mind is to always make sure that both the file name and the path given are correct. If either is incorrect or doesn’t exist, the error FileNotFoundError will be thrown, which needs to then be caught and handled by your program to prevent it from crashing.

To avoid this issue, as a best practice, errors can be caught with a try-finally block to handle the exception as shown below.

try:
    file_example = open ("F:DirectoryAnotherDirectorytestsTestingText.txt")
except IOError:
    print("An error was found. Either path is incorrect or file doesn't exist!")

finally:
    print("Terminating process!")

Reading from Files with read()

Python contains 3 functions to read files: read(), readline(), and readlines(). The last two functions are merely helper functions that make reading certain types of files easier.

For the examples that will be used, “TestingText.txt” contains the following text:

Hello, world! Python is the way to coding awesomeness.

If you don't believe me, try it on your own.

Come, you will enjoy the Dark Side. We have cookies!

The read method is used as follows:

file_example = open ("F:DirectoryAnotherDirectorytestsTestingText.txt", "r")

print(file_example.read())

The output will be as follows:

Hello, world! Python is the way to coding awesomeness.

If you don't believe me, try it on your own.

Come, you will enjoy the Dark Side. We have cookies!

Note: Special characters may not be read correctly using the read function. To read special characters correctly, you can pass the encoding parameter to read() function and set its value to utf8 as shown below:

file_example = open ("F:DirectoryAnotherDirectorytestsTestingText.txt", "r", encoding="utf8" )

Also, the function read(), as well as the helper-function readline(), can receive a number as a parameter that will determine the number of bytes to read from the file. In the case of a text file, this will be the number of characters returned.

file_example = open ("F:DirectoryAnotherDirectorytestsTestingText.txt", "r")

print(file_example.read(8))

The output will be as follows:

Hello, w

The helper-function readline() behaves in a similar manner, but instead of returning the whole text, it will return a single line.

file_example = open ("F:DirectoryAnotherDirectorytestsTestingText.txt", "r")

print(file_example.readline())
print(file_example.readline(5))

In the script above, the first print() statement returns the first line and inserts a blank line in the output console. The next print() statement is separated from the previous line with a blank line and starts on a new line as shown in the output:

Hello world! Python is the way to coding awesomeness.

If yo

Finally, the helper-function readlines() reads all of the text and splits them in to lines for easy reading. Take a look at the following example:

print(file_example.readlines())

The output from this code will be:

Hello world! Python is the way to coding awesomeness. If you don't believe me, try it on your own. Come, you will enjoy the Dark Side. We have cookies!

Keep in mind that the readlines() function is considered to be much slower and more inefficient than the read() function, without not many benefits. One good alternative to this is using a loop that will function much smoother and faster:

file_example = open ("F:DirectoryAnotherDirectorytestsTestingText.txt", "r")

for lines in file_example:
    print(lines) 

Note that if the line is not printed, it will be replaced in the buffer by the next statement

Writing to Files with write()

When using this function, any information inside the file with the same name will be overwritten. Its behavior is similar to the read() function, but inserting information rather than reading it.

file_example2 = open ("F:DirectoryAnotherDirectorytestsTestingTextTwo.txt", "w")

file_example2.write("This is a test. Enjoy it!n") #'n' is for change line.

file_example2.write("Another thing to know is doing it slowly.n")

file_example2.write("One by one. Yay!")

If several lines need to be written, the sub-function writelines() could be used instead:

listOfThingsToSay = ["I like things like: n", "Ice Creamn", "Fruitsn", "Moviesn", "Animen", "Napsn", "Jerkyn"]

file_example2 = open ("F:DirectoryAnotherDirectorytestsTestingTextTwo.txt", "w")

file_example2.writelines(listOfThingsToSay)

Note: to be able to use print() function, the mode needs to be set as w+, which allows reading as well as writing.

Adding to Files with append()

This function acts similar to the write() function, however, instead of overwriting the file, the append() function appends content to the existing file.

If a text file named “TestingTextThree” contains the following information:

Some essential things are missing in life and should not be avoided.

In order to append new text, the following code could be used:

listOfThingsDo = ["You need at least to: n", "Eat fried Ice Creamn", "Go to Disneyn", "Travel to the moonn", "Cook Pineapple Pizzan", "Dance Salsan"]

file_example3 = open ("F:DirectoryAnotherDirectorytestsTestingTextThree.txt", "a+")

file_example3.writelines(listOfThingsToDo)

for newline in file_example3
    print(newlines)

The output will be as follows:

Some essential things are missing in life and should not be avoided.

You need at least to:

Eat fried Ice Cream

Go to Disney

Travel to the moon

Cook Pineapple Pizza

Dance Salsa

Closing Opened Files with close()

The close() function clears the memory buffer and closes the file. This means that we’ll no longer be able to read from the file, and we’ll have to re-open it if we want to read from it again. Also, some operating systems, such as Windows, treat opened files as locked, so it is important to clean up after yourself within your code.

Using the previously used sample code, this function is used as follows:

file_example = open ("F:DirectoryAnotherDirectorytestsTestingText.txt", "r")

print(file_example.read())

file_example.close()

Conclusion

Python is one of the most robust programming languages and is also one of the most used. It is easy to implement as well as analyze, making it a perfect tool for beginners in programming. Furthermore, its versatility makes it a perfect starting point for programming novices.

In regards to file handling, Python has easy-to-use functions with rapid response time and relatively resilient error handling methods, so the development and debug processes are much more pain-free than in other languages when it comes to working with files.

Visit source site

Leave a Reply