Python 2.7 – Fundamentals – Files & Directories – File Handling

Let us look at the various file operations we can do in Python:

 

Opening Files – open()

Whether you want to write data to a file or read data from it, the first step is to open it.

The open function lets you open a file. It takes in two arguments – the path to the file and the mode string. The open function returns a file object. This file object is then used for the other file operations.

Eg:

>>> 
>>> f = open("test.txt")
>>> 

If the mode is not specified, it is assumed that the file is open for reading. So if “test.txt” does not exist in the current directory it will throw an exception. The following modes are there:

  • ‘r’ – open for reading
  • ‘w’ – open for writing. If file exists, truncate it else create a new one
  • ‘a’ – append to the end of an existing file. If file does not exist, create it
  • ‘t’ – open in text mode
  • ‘b’ – open in binary mode

The mode strings can be used together as shown below:

>>>
 >>> f = open("test.txt", "r+t")
 >>>
 >>> f = open("image.png", "w+b")

 

Closing Files – close()

Every file which is opened must be closed so as to free up the file resources:

>> 
>>> f = open("test.txt") 
>>> f.close()
>>> 
>>>

 

Writing Files – write()

Once a file has been opened using the “w” or “a” mode, we use the write() method to write data to it. An example is given below:

>>> 
>>> 
>>> f = open("write.dat", "w")
>>> f.write("This is a sample text")
>>> f.write(".One more line of text")
>>> f.close()
>>>

The above code writes string data to the file. If you want to write data in the form of lines then each string data should be terminated with a newline \n character:

>>> f = open("write.dat", "r")
>>> f.close()
>>> f = open("write2.dat", "w")
>>> f.write("This is a sample text.\n")
>>> f.write("One more line of text\n")
>>> f.write("Another line of text\n")
>>> f.close()
>>>

Lets see how the data looks when we open the same file for reading.

 

Reading Files – read(), tell(), seek()

Once a file is open for reading, we use the read method to read the data. The read() method can take in an optional argument of how many characters to read. eg. read(2) If no argument is given then it will read all the contents of the file:

>>> f = open("write.dat")
>>> f.read(10)
'This is a '
>>> f.read(10)
'sample tex'
>>> f.read()
't.One more line of text'
>>> f.close()
>>> f = open("write.dat")
>>> f.read()
'This is a sample text.One more line of text'
>>> 
>>> f.close()
>>> f = open("write2.dat")
>>> f.read()
'This is a sample text.\nOne more line of text\nAnother line of text\n'
>>> f.close()
>>>

In the case of write2.dat we can see that the newline character has been added to each line.

The file cursor is a pointer which keeps moving ahead every time the read() method is called. When you first open a file, the cursor is at position 0. f.read(4) will move the file cursor to the 4th character. Doing another f.read(4) will move the cursor to the 8th character in the file. Once the read() has reached the end of the file, the file cursor will be at the last character of the file. We can check the position of the file cursor by using the f.tell() method:

>>> f = open("write.dat")
>>> f.tell()
0L
>>> f.read(10)
'This is a '
>>> f.tell()
10L
>>> f.read(10)
'sample tex'
>>> f.tell()
20L
>>> f.read(10)
't.One more'
>>> f.tell()
30L
>>> f.read(10)
' line of t'
>>> f.tell()
40L
>>> f.read(10)
'ext'
>>> f.tell()
43L
>>> f.close()
>>>

We can move the file cursor to any position we want by using the seek() method:

>>> f = open("write.dat")
>>> f.read()
'This is a sample text.One more line of text'
>>> f.seek(10)
>>> f.read()
'sample text.One more line of text'
>>> f.seek(2)
>>> f.read(4)
'is i'
>>> f.seek(100)
>>> f.read()
''
>>> f.close()
>>>

The seek() method takes in a single argument on where to place the file cursor. 0 will put the cursor at the beginning of the file. Putting an argument more than the length of the characters in the file will put the file cursor at the end.

 

Apart from the basic methods explained above the file object has many other methods. Some of the simpler and relevant ones are mentioned below:

  • readline(max characters) – read in an entire line of data if no argument is given. The max characters specify how many max characters to read in the line.
  • readlines(max characters) – reads in all the lines in the file if no argument is given. The max characters argument in this case will specify how many max characters to read from the file to at least complete the next line. This is only useful for large files with a lot of data. It has no effect on small files.

In the next post , we will look at Functions.

 

 

 

1 Trackback / Pingback

  1. Python 2.7 – Fundamentals – Files & Directories – Directory Management – Truelogic Blog

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.