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, 21 November 2016

Python File Object Attributes and Methods - readlines(), seek(), etc.

Python file object attributes and methods - This is the 15th article of our tutorial series on Python and it is all about most frequently used attributes and methods related to Python file objects. In the last article on Python file objects, we've learned how file objects can be created with open() function and did basic operations on them with methods like read() and write(). In this article, we move a step further and know about useful attributes and methods associated with Python file objects.

python-file-object-attributes-and-methods

To begin with, we create a file object using open() function and get a list of all possible methods that can be used with a file object, using Python built-in dir() function. As you might know by now that, the dir() function accepts a Python object as an argument and returns a list of attributes and methods related to them. Lets check this.

>>> myFileObject = open('/root/myFile.txt')
>>> dir(myFileObject)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']

In above output, we can see some attributes whose name starts and ends with double underscores in the form __attrName__, which are called as Magic attributes. These are used by Python internally and we do not discuss over them in this article. So, lets start our discussion on some more commonly used Python file object methods.

1. close() Method :

As we observed in our last article, close() method will close the connection between a file and a file object, such that the file operations cannot be performed using a file object. It will also release the system resources consumed by the file object. But, the file object will still be present in the memory until it is deleted. The syntax to use this method is - file_object_name.close()

# We create a file object
>>> myFileObject = open('/root/myFile.txt')
>>> myFileObject
<open file '/root/myFile.txt', mode 'r' at 0x7f574e8e95d0>

# close() method is called on the file object
>>> myFileObject.close()

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

# File object still exists after closing the file
>>> myFileObject
<closed file '/root/myFile.txt', mode 'r' at 0x7f574e8e95d0>

2. closed Attribute :

This attribute when qualified with a file object returns True if file state is closed, else False. This being the first time we are using an attribute, we must know how it is used. Attributes, just like methods, must be qualified with the object names. But, we don't use parenthesis while using an attribute. So, if the syntax for using a method is object_name.method_name(), then the syntax to use an attribute is simply object_name.attribute_name.

>>> myFileObject = open('/root/myFile.txt')
>>> myFileObject
<open file '/root/myFile.txt', mode 'r' at 0x7f574e8e9660>
>>> myFileObject.closed
False

>>> myFileObject.close()
>>> myFileObject
<closed file '/root/myFile.txt', mode 'r' at 0x7f574e8e9660>
>>> myFileObject.closed
True

3. mode Attribute :

This attribute when used with a file object, returns the mode in which the file is opened.

>>> myFileObject = open('/root/myFile.txt', 'r')
>>> myFileObject.mode
'r'

>>> myFileObject = open('/root/myFile.txt', 'w')
>>> myFileObject.mode
'w'

>>> myFileObject = open('/root/myFile.txt', 'a')
>>> myFileObject.mode
'a'

4. name Attribute :

This attribute, as the name suggests, returns the name of the file which the file object is linked to.

>>> myFileObject = open('/root/myFile.txt')
>>> myFileObject.name
'/root/myFile.txt'

5. next() Method :

With this method, we can iterate over the lines in the file. This method when qualified with a file object, returns the next line. This means, when we call this method for the first time, it would return the first line. When last line is returned and next() method is called, it would raise StopIteration exception.

>>> myFileObject = open('/root/myFile.txt', 'r')
>>> line = myFileObject.next()
>>> print line
This is line no. 1.

>>> line = myFileObject.next()
>>> print line
This is line no. 2.

>>> line = myFileObject.next()
>>> print line
This is line no. 3.

>>> line = myFileObject.next()
>>> print line
This is line no. 4.

>>> line = myFileObject.next()
>>> print line
This is line no. 5.

# Last line has been printed and we now call next() method again
>>> line = myFileObject.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

6. read() Method :

We've already used this method in our last tutorial. This method when called on a file objects, all the file contents are returned as a string. It does accept an optional argument N which defines the number of bytes or characters to be read fro the file. So, the syntax for using this method becomes - file_object_name.read([N])

>>> myFileObject = open('/root/myFile.txt', 'r')
>>> fileData = myFileObject.read()
>>> print fileData
This is line no. 1.
This is line no. 2.
This is line no. 3.
This is line no. 4.
This is line no. 5.

>>> myFileObject = open('/root/myFile.txt', 'r')
>>> fileData = myFileObject.read(25)
>>> print fileData
This is line no. 1.
This

7. readline() Method :

This method when called on a file object, reads a line from a file and returns it in a string format. Like read() method, it does accept an optional parameter N, where N is the number of characters to read from the file.

>>> myFileObject = open('/root/myFile.txt', 'r')
>>> line = myFileObject.readline()
>>> print line
This is line no. 1.

>>> line = myFileObject.readline()
>>> print line
This is line no. 2.

>>> line = myFileObject.readline()
>>> print line
This is line no. 3.

>>> line = myFileObject.readline(5)
>>> print line
This
>>> line = myFileObject.readline(12)
>>> print line
is line no.

8. readlines() Method :

This method when called on a file object, returns a list in which each list item is a string and each string is a line from the file. So, we have entire file contents stored in a list, with each list item is a line from the file stored as a string.

>>> myFileObject = open('/root/myFile.txt')
>>> fileData = myFileObject.readlines()
>>> fileData
['This is line no. 1.\n', 'This is line no. 2.\n', 'This is line no. 3.\n', 'This is line no. 4.\n', 'This is line no. 5.\n']

9. seek() Method :

Whenever we create a file object for a file, suppose that, we have a pointer which keeps record of our current position in the opened file. For a newly created file object, its default value is '0' i.e. start of the file. With this method, we can move this pointer from one position to another within a file. This method accepts one mandatory argument offset which tells us that we need to move the pointer by these many places. It also accepts an optional argument which specifies the direction of movement of the pointer, for which valid values are '0', '1' and '2'. '0' is the default value and it specifies that the movement should be from absolute position i.e. from the beginning of the file. '1' indicates that the movement should from relative position i.e. from the current position. '2' signifies the movement to the offset with reference to the end of the file i.e. backward movement. To verify this, we should learn tell() method first.

10. tell() Method :

As we already know that, our current position in the opened file is tracked by a pointer. With tell(), we can get to know about our current position in that opened file. We now have a look at some examples which will give us a clear view about seek() and tell() methods.

>>> myFileObject = open('/root/myFile.txt')

# Default position is '0'
>>> myFileObject.tell()
0

# Second argument defaults to '0'
>>> myFileObject.seek(128)
>>> myFileObject.tell()
128
>>> myFileObject.seek(256)
>>> myFileObject.tell()
256

# Moving 128 bytes from the beginning
>>> myFileObject.seek(128, 0)
>>> myFileObject.tell()
128

# Moving 128 bytes from current position
>>> myFileObject.seek(128, 1)
>>> myFileObject.tell()
256

# Moving 64 bytes backward from the end of the file
>>> myFileObject.seek(-64, 2)
>>> myFileObject.tell()
36

# Reading the file from this position
>>> myFileObject.read()
' 2.\nThis is line no. 3.\nThis is line no. 4.\nThis is line no. 5.\n'

11. truncate() Method :

This method when called on a file object, it truncates (flushes) the data from the file. Remember, when we open a file in 'w', it flushes all the data by default. So, we can use read-write mode here to bypass default truncating process. Till this point, we have used 'r', 'w' and 'a' modes. We now introduce to some new modes that can be used in open() function.
  • 'r+' - It does not truncate the file completely (as in case of 'w' mode), but opens the file in 'read-write' mode, with the current position '0' in the opened file.
  • 'w+' - File is opened in 'read-write' mode and truncated.
  • 'a+' - File is opened in 'read-write' mode, file is not truncated, with current position at the end in the opened file.
# 'r+' mode is 'read-write' mode
>>> myFileObject = open('/root/myFile.txt', 'r+')
>>> myFileObject
<open file '/root/myFile.txt', mode 'r+' at 0x7ff29220a5d0>
>>> myFileObject.truncate()
>>> myFileObject.close()

# We verify this with 'cat' command in Linux and we observe an empty file
$ cat myFile.txt
$

12. write() Method :

This method accepts data in string format as an argument and writes the data to a file, if it is opened in 'w' mode, or appends the data to the file, if it is opened in 'a' mode.

>>> myFileObject = open('/root/myFile.txt', 'w')
>>> data = 'This is line no. 1.\n'
>>> myFileObject.write(data)
>>> data = 'This is line no. 2.\n'
>>> myFileObject.write(data)
>>> data = 'This is line no. 3.\n'
>>> myFileObject.write(data)
>>> data = 'This is line no. 4.\n'
>>> myFileObject.write(data)
>>> data = 'This is line no. 5.\n'
>>> myFileObject.write(data)
>>> myFileObject.close()
>>> myFileObject = open('/root/myFile.txt', 'r')

>>> myFileObject.read()
'This is line no. 1.\nThis is line no. 2.\nThis is line no. 3.\nThis is line no. 4.\nThis is line no. 5.\n'

13. writelines() Method :

This method accepts an iterable wherein an item is a str type, typically a list of strings or a tuple of strings, and writes the data into the file.

>>> myFileObject = open('/root/myFile.txt', 'w')
>>> myDataList = ['This is line no. 1.\n', 'This is line no. 2.\n', 'This is line no. 3.\n', 'This is line no. 4.\n', 'This is line no. 5.\n']
>>> myDataTuple = ('This is line no. 6.\n', 'This is line no. 7.\n', 'This is line no. 8.\n', 'This is line no. 9.\n', 'This is line no. 10.\n')
>>> myFileObject.writelines(myDataList)
>>> myFileObject.writelines(myDataTuple)
>>> myFileObject.close()
>>> myFileObject = open('/root/myFile.txt', 'r')

>>> myFileObject.read()
'This is line no. 1.\nThis is line no. 2.\nThis is line no. 3.\nThis is line no. 4.\nThis is line no. 5.\nThis is line no. 6.\nThis is line no. 7.\nThis is line no. 8.\nThis is line no. 9.\nThis is line no. 10.\n'

With this, we have come to an end of this discussion. In this article, we've learned some important and most commonly used file object attributes and methods, with their examples. In the next article, we will be learning about another Python data structure - Sets. Please post your views and feedback about this article in the comment section below and stay tuned for more articles on Python. Thank you.

This article is originally published at www.codeninja.in - Python File Object Attributes and Methods

0 comments:

Post a Comment