Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Monday, 14 November 2016

Python File Handling - Open, Read, Write and Append to file

Python file handling : Open, Read, Write and Append to file - Hello readers! This is the 14th article of our Python tutorial series and we will be having an introduction to Python Files and File objects in this article. So far, we have covered Python Strings, Python Lists, Python Dictionaries and Python Tuples in earlier articles. These data structures were mostly numbers, strings, sequences of Python objects or key-value pairs of Python objects. But, File object in Python is very different from them, as it is more related to file handling. So, the operations related to file objects are also different from other Python objects, same as the operations we do with files. When I mean to say a file, it is actually the file present on some filesystem in your computer. You can consider a file object as a gateway to the actual file, with which we can read/write/append data from/to that file. So, without much a do, we start our discussion on Python file objects straight away.

python-open-read-write-append-to-file

As mentioned earlier, Python file objects are the connectors to the file on your machine. If we have to read data from file, write or append something to the file, we have to use these file objects. Needless to mention, like everything else in Python, file objects are also Python objects and they belong to type file or in other words, every file object is the instance of the class file. In order to create a file object, we have to use a Python built-in function open(). This function returns a file object, when provided with file name as the argument. It being an instance of file class, it has some methods associated with it, which we would cover later. For now, let us create our first file object. To do so, I would use the log file /var/log/messages and pass it as an argument to open() function as below:

>>> myFileObject = open('/var/log/messages')
>>> type(myFileObject)
<type 'file'>
>>> myFileObject
<open file '/var/log/messages', mode 'r' at 0x7f13121ee5d0>

As you can see on the last line, when we type the name of file object, it provides us some useful information - the file name, the mode in which it is opened and the memory location of the file object. In above example, we have provided only a single argument to the open() function. But, in fact, it can accept up to three arguments - a file name, the mode and buffering, out of which only first two are more commonly used. Among these two arguments, former (file name) is mandatory and later (mode) is optional. The file name argument is the absolute or relative path of the file, if path is not provided, file will be created in present (or current) working directory. The mode attribute defines the mode in which the file has been opened. There are three modes in which a file can be opened - 'r' (Read mode), 'w' (Write mode) and 'a' (Append mode). Please note that, both these arguments to open() function are of 'str' type.

The Read mode is the default mode and used when you just need to read the contents from a file. Opening a file in Write mode will create a new file (or if it's already there, it will overwrite it's contents) and start writing to it. When Append mode is used, file contents are preserved and data can be added to the end of the file contents. Lets not go to terminal and check how open() function works.

# Opening a file in 'Read' mode
>>> myFileObject = open('/var/log/messages', 'r')
>>> myFileObject
<open file '/var/log/messages', mode 'r' at 0x7f6bb84b85d0>

# 'Read' mode is the default mode
>>> myFileObject = open('/var/log/messages')
>>> myFileObject
<open file '/var/log/messages', mode 'r' at 0x7f6bb84b8660>

# Opening a file in 'Write' mode
>>> myFileObject = open('/root/myFile.txt', 'w')
>>> myFileObject
<open file '/root/myFile.txt', mode 'w' at 0x7f6bb84b85d0>

# Opening a file in 'Append' mode
>>> myFileObject = open('/root/myFile.txt', 'a')
>>> myFileObject
<open file '/root/myFile.txt', mode 'a' at 0x7f6bb84b8660>

In above examples, we have just verified that the open() function actually creates the object we wish to create.We are now able to create a Python file object that can read from a file, write to a file or append to a file. But, we haven't done any I/O operation yet. For this, we must know about two important file methods - read() and write. Both of these methods are called on file objects.

The read() method when called on a Python file object, reads the entire file and returns a string containing file contents. It does accept an optional parameter N, where N is the number of bytes or characters to be read from the file. While, write() method accepts a mandatory argument data of str type and writes that string to the file. We have to use write() method, when the file is opened in 'w' mode or in 'a' mode. To demonstrate this, let's create a file object, write some data to it, append some more and read everything from the file.

# We write two lines to the file 'myFile.txt'
>>> myFileObject = open('/root/myFile.txt', 'w')
>>> myDataString = 'This is the 1st line.\n'
>>> myFileObject.write(myDataString)
>>> myFileObject.write('This is the 2nd one.\n')

# We verify the contents of the file using 'cat' command in Linux
$ cat /root/myFile.txt
This is the 1st line.
This is the 2nd one.

# We append two lines to the file
>>> myFileObject = open('/root/myFile.txt', 'a')
>>> myDataString = 'This is the 1st appended line.\n'
>>> myFileObject.write(myDataString)
>>> myFileObject.write('This is the 2nd appended line.\n')

# We verify the file contents using 'cat' command in Linux
$ cat /root/myFile.txt
This is the 1st line.
This is the 2nd one.
This is the 1st appended line.
This is the 2nd appended line.

>>> myFileObject = open('/root/myFile.txt', 'r')
>>> myFileData = myFileObject.read()
>>> print myFileData
This is the 1st line.
This is the 2nd one.
This is the 1st appended line.
This is the 2nd appended line.

# We use the syntax 'read(N)' to read first 'N' characters from the file
>>> myFileObject = open('/root/myFile.txt', 'r')
>>> myFileData1 = myFileObject.read(30)
>>> print myFileData1
This is the 1st line.
This is

When we create a file object using open() function, Python reserves some system resources to store this object. In the earlier part of this article, I've defined file objects as the connectors to the files present on the filesystem of your computer. This means that, whenever we are done working on a file and corresponding file object, the connection must be closed, so that the resources consumed by the file object would be released. This process is done automatically when we come out of Python interpreter or our Python program exits. But, a good programmer will always do it manually and that can be done by calling close() method on the file object. It would free up all the resources reserved by the file object on which the method is called. It won't delete the file object myFileObject though, as we are just closing the connection, until it is deleted manually.

# We open a file for reading
>>> myFileObject = open('/root/myFile.txt', 'r')
>>> myFileObject
<open file '/root/myFile.txt', mode 'r' at 0x7f0d2e0855d0>
>>> # We perform some operations here...
...

# We close the file after performing some operations
>>> myFileObject.close()

# We try to perform read operation on a closed file
>>> myDataString = myFileObject.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

# File object is not deleted
>>> myFileObject
<closed file '/root/myFile.txt', mode 'r' at 0x7f0d2e0855d0>

With this, we have come to an end of this article. In this article, we have had a brief introduction to Python file objects. We've learned to create file objects with Python built-in open() function. Along with that, we have come across some very useful methods, viz. read() and write(), that can be called on file objects to perform I/O operations on files. And at last, we've seen how close() method can be used to free up system resources. In the next article, we would be learning about some more file object methods. Please share your feedback in the comment section below and stay tuned. Thank you!

This article is originally published at www.codeninja.in - Python Files and File Objects

1 comment:

  1. `read(n)` function read **bytes** not characters! Like most of current files are encoded in utf8, one should do `read().decode("utf-8")` for instance to get a real string. Of course for ASCII characters only, one byte = one char, but this only true for this charset.

    ReplyDelete