File Objects in Python

DIQ Network

This article is an introduction to File I/O in Python. It discusses the methods and attributes you can use to manipulate and get information about file objects. The OS and Pickle modules are also explained.

I was planning to write about some of the advanced object oriented programming techniques in Python, a subject that is not quite authored. But an email by Harish Kumar, a Python newbie and a Dot Net professional, opened my eyes to some of the other subjects, which I felt needed a detailed discussion.

Python is a language that handles file related operations extremely well. But it is a not-quite-explored feature. Python handles file operations just the way you read and write from a book. Python creates a file object, when you open a file. You can manipulate a file, by operating on the file object created by it. I created a simple file called ‘diq.txt’ using the Notepad editor. I wrote a sentence with contents “I love You” inside the file. Then I saved it in the root folder (C:\) of a Windows 98 system. Users of Linux can save it anywhere, but make sure that you provide the path properly. You can create a file object in Python using this command:

>>> file= open  ("C:\diq.txt", "w")
>>> type(file)
<type 'file'>
>>> print file
<open file  'C:\diq.txt', mode 'w' at 0x01175220>
>>>

It is clear that a file is opened and it is opened in the ‘write’ mode. The ‘open’ command in Python takes two arguments. One is the name of the file, and the other is the mode in which it needs to be opened. The most common are the ‘write’ and ‘read’ modes. You have to be careful and note that the arguments have to be presented in quotes. Now when you use the ‘open’ command with ‘write’ as the option, you need to note that it will either create a new file or overwrite an existing file if a file with the same name is found. You can write some content into the file by using the ‘write’ method in the ‘file’ object. You can close the file after writing the new content using the ‘close’ method.

>>>file.write("I  love Developer IQ!")
>>> file.close()
>>> print file
<closed file  'C:\diq.txt', mode 'w' at 0x011A55A0>

Now let us read the file into which we have written some new content. You have to open it for reading.

>>> file=  open("C:\diq.txt", "r")
>>> content=  file.read()
>>> print content
I love Developer IQ!

As you may note, the output is the new content that has been written. The old file, which you had created using Notepad is replaced by the one created from our Python program. If you have doubts, try reading the contents using Notepad. The above exercise gives you an idea of the power of Python as far as file manipulation goes. In Linux you need to set ‘chmod’ options of files properly, otherwise errors can be thrown up!

Remember your school days! I do not know about my readers, but I have been a few times caught by my teachers, and have been asked to write ‘imposition’, which meant that I needed to write some sentence, passage from a book, or something even more boring fifty or a hundred times. I will now write a Python program (Code-1) that will let you write impositions with a few minutes of coding.

Code 1

>>> def  impos(string, number):
        i=0
        f=open("impose.txt",  "w")
        while  i < number:
        f.write(string)
        i=i+1
        f.close()

 

>>> impos("I love Python", 10) >>> f= open("impose.txt", "r") >>> content=f.read()
>>> print content

I love PythonI love PythonI love PythonI love PythonI love PythonI love PythonI love PythonI love PythonI love PythonI love Python

You can check for the text file ‘impose.txt’ physically and find that the content is copied. What I have done is that I have used the ‘write’ method within a simple While loop, to create a file with a specific content, repeated number of times. However you may want to have ‘I love Python’ written in separate lines. I have modified the program (Code-2) to accommodate that change with a bit of imagination as given below.

Code 2

>>> def  impos(string, number):
        i=0
        str=string+'\n'
        f=open("impose.txt",  "w")
        while  i < number:
        f.write(str)
        i=i+1
    f.close()

Some of the methods under file objects

There are several methods which come under file objects. A discussion on all methods is beyond the scope of this article. Here, we will discuss some of the common methods that you will find useful in programming. The methods seen in file objects are actually inspired from C language, and implementation is very similar. To get a hang of it, refer to your Python manual. Some of these are illustrated in the listing below (Code-3). An explanation is provided as comments

Code 3

>>>  f.write("Python rocks!") # string contents written to file
  >>> f.close() #  file is closed
  >>>  f=open("raja.txt", "r") # file opened in read mode
  >>> f.closed #  checks whether the file is closed or not, returns Boolean
False
>>> f.fileno()# returns the integer – file descriptor 4
>>> f.isatty() # checks whether file is connected to a tty device or not
False
>>> f.mode # checks and returns what mode the file is 'r'
>>> f.name # checks and returns the name of the file 'raja.txt'
>>> f.next() # to step over the lines of a file 'Python rocks!'
>>> f.tell() # returns files current position 13L

There are a few more methods, which deserve a better explanation. I will do that after I introduce to you some of the other modules in Python that are fairly useful when it comes to file and I/O operations.

The OS and Pickle modules

The OS and Pickle modules have methods and properties, which are quite useful in writing a bit more advanced programs. The OS module as the name suggests is a module, which has a huge number of OS related classes, methods and properties. Pickle, which gets its name from the fact that pickling means ‘perserving’ is used to manipulate or preserve data structures inside files.

If you explore these two modules you will find a wealth of methods that will take probably weeks for you to explore. I will illustrate how you can use some of these methods with an example. We will try to create a directory and a file inside it. To make the program simple I have only used a single directory and filename. See Code-4.

The method ‘join’ inside ‘os.path’ can be used to create paths in both Unix and Windows. I have written this program for Windows, and specifically for C:\. Please note, when you pass arguments to the function, always enter the names in quotes. The comments that I have provided must be self-explanatory. Refer to the manuals for more.

Code 4

def createfile( dir,  file):
        import os # imports os module
        dirpath=os.path.join('c:',dir) # creates the  directory path with root
if os.path.exists(dirpath): filename=os.path.join(dirpath, file) # creates file name path
f=open(filename,'w') # creates file f.write('I Love Python') # inputs content f.close() # closes file print "New file has been created” else:
os.mkdir(dirpath, 511) # creates new directory createfile(dir,file) # creates directory print ‘New Directory has been created’

It is very important to discuss the ‘readline’ and ‘readlines’ methods in the file objects. The ‘readline’ method reads one line at a time. The first call to ‘readline’ returns the first line in the file object, everything up to and including the first newline character, or the entire file if there is no newline character in the file. The ‘readlines’ method reads all the lines in a file and then returns them in a list with each line as an item of the list. I will now write a program (Code-5) to count the number of lines using ‘readline’. The program makes use of the fact that if ‘readline’ method generates an empty string, which means that no more content exists inside a file, it has reached the end of the document.

Code 5

>>> def  countlines(filename):
        i=0
        f=open(filename,  'r')
        while  f.readline() != '':
        i=i+1
    print  i

When you run such programs on Windows make sure that you do not use any slashes to differentiate the drives from directories. Hence ‘C:\raja.txt’ is passed on as ‘c:raja.txt’.

You could actually make this program a lot simpler if you were using the ‘readlines’ method. Remember that the ‘readlines’ method throws up a list of strings containing each line as an item inside the list. You can hence achieve that by simply finding the length of an object pointed by the ‘readlines’ method.

>>>  x=f.readlines()
>>>  len(x)

We had an introduction of Python’s file objects. We also took a look at some of the methods from other modules in Python which helps you to do more creative programming in the language. In the next article we will delve deeper into the ‘Pickle’ and ‘struct’ modules in Python, and write a few more interesting programs.

I wish to bring to the readers’ notice that all the programs in this article have been tested only on the Windows environment. With minor modifications you can run these programs on Linux. Though most Python developers are Linux users, the popularity of the programming language in Windows is growing at exponential rates. I would appreciate if any of the readers can test and modify programs for Linux and then mail it to me, so that I can share it with our other readers.








}