Python Devlopment






Text Files



File is a named location on disk to store related information. It is used to permanently store data in a non-volatile memory (e.g. hard disk).

Since, random access memory (RAM) is volatile which loses its data when computer is turned off, we use files for future use of the data.

When we want to read from or write to a file we need to open it first. When we are done, it needs to be closed, so that resources that are tied with the file are freed.

Hence, in Python, a file operation takes place in the following order.

  1. Open a file
  2. Read or write (perform operation)
  3. Close the file

1. Opening a File

Python has a built-in function open() to open a file from directory.Two arguments that are mainly needed by the open () function are: file name or file path and mode in which the file is opened.  This function returns a file object, also called a handle, as it is used to read or modify the file accordingly.

The synax for opening a file is:

file_object=open(file_name or path, [access_mode])

 

file_name : File name contains a string type value containing the name of the file which we want to access.

access_mode : The value of access_mode specifies the mode in which we want to open the file i.e. read,write, append etc. The default access_mode is r (reading)

 

>>> f = open("F:\Harish\Python\file\xyz.txt")  # specifying full path
>>> f = open("abc.txt")    # open file in current directory
>>> f
<_io.TextIOWrapper name='abc.txt' mode='r' encoding='cp1252'>

 

We can specify the mode while opening a file. In mode, we specify whether we want to read 'r', write 'w' or append 'a' to the file. We also specify if we want to open the file in text mode or binary mode.

The default is reading in text mode. In this mode, we get strings when reading from the file.

On the other hand, binary mode returns bytes and this is the mode to be used when dealing with non-text files like image or exe files.

Python file modes

'r' Open a file for reading mode. The pointer is placed at the starting of the file. It is a default mode
'w' Open a file for writing only mode. If a file exists, it overwrites the existing file; otherwise it creates a new file
'r+' It opens a file in both reading and writing mode. The file pointer is placed at the starting of the file
'w+' It opens a file in bothe reading and writing mode. If a file exists, it overwrites the existing file; otherwise it creates a new file
'a' It opens a file for appending. The file pointer is placed at the end of the file. If the file doesnot exists in the directory, it creates a new file for writing.
'a+' It opens a file for appending and reading. The file pointer is placed at the end of the file. If the file doesnot exists in the directory, it creates a new file for writing.
'rb' Open a file for reading only mode in binary format. The pointer is placed at the starting of the file.
'rb+' Open a file reading and writing mode in binary format. The pointer is placed at the starting of the file.
'wb' Open a file for writing mode in binary format. If a file exists, it overwrites the existing file; otherwise it creates a new file
'wb+' Open a file reading and writing mode in binary format. If a file exists, it overwrites the existing file; otherwise it creates a new file.
'ab' It opens a file for appending in binary mode. The file pointer is placed at the end of the file. If the file doesnot exists in the directory, it creates a new file for writing
'ab+' It opens a file for appending and reading in binary mode. The file pointer is placed at the end of the file. If the file doesnot exists in the directory, it creates a new file for writing

 

f = open("abc.txt")         # equivalent to 'r' or 'rt'
f = open("abc.txt",'w')      # write in text mode
f = open("img.bmp",'r+b')      # read and write in binary mode

 

Unlike other languages, the character 'a' does not imply the number 97 until it is encoded using ASCII (or other equivalent encodings).

Moreover, the default encoding is platform dependent. In windows, it is 'cp1252' but 'utf-8' in Linux.

So, we must not also rely on the default encoding or else our code will behave differently in different platforms.

Hence, when working with files in text mode, it is highly recommended to specify the encoding type.

f = open("abc.txt",mode = 'r',encoding = 'utf-8')

 

2. Closing a File

When we are done with operations to the file, we need to properly close the file.

Closing a file will free up the resources that were tied with the file and is done using Python close() method.

Python has a garbage collector to clean up unreferenced objects but, we must not rely on it to close the file.

The synatx of closing a file is

fileObject.close()

Example:

#Open a file
>>>f=open("abc.txt" , "wb")
# Perform file operations
>>>f.close()            #Closing the file

This method is not entirely safe. If an exception occurs when we are performing some operation with the file, the code exits without closing the file.

 

3. Reading a file

The syntax for readind a file is 

fileObject.read([size])

Here, size is optional, if size is provided, then that amount of data will be considered for reading.

 

Followings are the steps to read existing filele. 

1. Create a text file in any folder and save it e.g. "abc.txt".

2. Double click on on "abc.txt" file and type some contents in that file

     for example the contents in file "abc.txt" are  

First Line    
Second Line    
Third Line

3. Now using read() method we can open the created file "abc.txt" and view the contents

f=open("abc.txt","r")
text=f.read()
print(text)

#Output
First Line
Second Line
Third Line
>>> 

In the above example, we have created file manually and the read that file using read() method.

 

4. Wrting to a File

In order to write into a file in Python, we need to open it in write 'w' or append 'a' mode.

We need to be careful with the 'w' mode as it will overwrite into the file if it already exists. All previous data are erased.

Writing a string or sequence of bytes (for binary files) is done using write() method. This method returns the number of characters written to the file

The synatx of write() method is

fileObject.write(string)

 

The content that we want to write to a file is passed as a parameter to the above syntax

 

#Open a file with w mode
f=open("abc.txt","w")
f.write("First Line\n")    
f.write("Second Line\n")    
f.write("Third Line\n")
f.close()        #Close the file after writing

This program will create a new file named 'abc.txt' if it does not exist. If it does exist, it is overwritten.

We must include the newline characters ourselves to distinguish different lines.

 

4. Renaming File

Renaming a file in python is done with the help of rename() method. The rename() method is passed with two arguments, the current file and the new file.

Synax

os.rename(current_filename, new_filename)

 

For Example, consider a file which has name "abc.txt" and we want to rename it to "xyz.txt". for this we need to import os in your program.

import os
os.rename("abc.txt","xyz.txt")

Here the file "abc.txt" will be renamed to "xyz.txt".

 

5. Deleting a File

Deleting a file in Python is done with the help of the remove() method. It takes the filename as an argument to be deleted.

The synax is

os.remove(filename)

 

For example, the file "abc.txt" we want to delete, then it would be deleted using above remove() method.

import os
#Deleting the file
os.remove("abc.txt")

After running above program, the file "abc.txt" will be deleted. 

 

6. File Related Methods

The file object in Python provides various methods to manipulate files. some of the methods which are listed as follows.

1. file.close()                : After performing operations, it close the file.

2. file.flush()                : It flushes the internal buffer memory.

3. file.fileno()               : It returns the integer file descripter

4. file.next()                 : It returns the next line from the file

5. file.read([size])        : Reads the size bytes from a file

6. file.readline([size])  : It reads the entire one line from a file

7. file.seek([offset)       : It changes the current position

8. file.tell()                   : It returns the file's current position

9. file.truncate([size])  : It truncates the file

10. file.write(str)          : It write the string to the file  

Examples:

1. Write a function that prints the contents of a file in uppercase. The function should accept the filename as argument.

def upper(filename):
    infile = open(filename)
    for line in infile:
         print line.upper()
         infile.close()
         upper("test.txt")

 

2. Write a function that sorts that contents of the file. The function should accept the filename as argument.

def sort(filename):
    infile = open(filename)
    lines = infile.readlines()
    infile.close()
    lines.sort()
    outfile = open(filename, "w")
    outfile.writelines(lines)
    outfile.close()
    sort("test.txt")