Ticker

6/recent/ticker-posts

Python: File Handling

Python: File Handling

As the part of programming requirement, we have to store our data permanently for future purpose. For this requirement, we should go for files.

Files are very common permanent storage areas to store our data.

Types of Files:

There are 2 types of files

1. Text Files:

Usually we can use text files to store character data 

eg: abc.txt

2. Binary Files:

Usually we can use binary files to store binary data like images,video files, audio files etc...

Opening a File:

Before performing any operation (like read or write) on the file,first we have to open that file.For this we should use Python's inbuilt function open()

But at the time of open, we have to specify mode,which represents the purpose of opening file.

f = open(filename, mode)

The allowed modes in Python are

1. r => open an existing file for read operation. The file pointer is positioned at the beginning of the file.If the specified file does not exist then we will get FileNotFoundError.This is default mode.

2. w => open an existing file for write operation. If the file already contains some data then it will be overridden. If the specified file is not already avaialble then this mode will create that file.

3. a => open an existing file for append operation. It won't override existing data.If the specified file is not already avaialble then this mode will create a new file.

4. r+ => To read and write data into the file. The previous data in the file will not be deleted.The file pointer is placed at the beginning of the file.

5. w+ => To write and read data. It will override existing data.

6. a+ => To append and read data from the file.It wont override existing data.

7. x => To open a file in exclusive creation mode for write operation. If the file already exists then we will get FileExistsError.

Closing a File:

After completing our operations on the file,it is highly recommended to close the file.

For this we have to use close() function.

f.close()

Various properties of File Object:

Once we opend a file and we got file object,we can get various details related to that file by using its properties.

name => Name of opened file

mode => Mode in which the file is opened

closed => Returns boolean value indicates that file is closed or not

readable()=> Retruns boolean value indicates that whether file is readable or not

writable()=> Returns boolean value indicates that whether file is writable or not.

Eg:

1) f=open("abc.txt",'w') 
2) print("File Name: ",f.name) 
3) print("File Mode: ",f.mode) 
4) print("Is File Readable: ",f.readable()) 
5) print("Is File Writable: ",f.writable()) 
6) print("Is File Closed : ",f.closed) 
7) f.close() 
8) print("Is File Closed : ",f.closed) 
9) 
10) 
11) Output
12) D:\Python_classes>py test.py 
13) File Name: abc.txt 
14) File Mode: w 
15) Is File Readable: False 
16) Is File Writable: True 
17) Is File Closed : False 
18) Is File Closed : True 

Writing data to text files:

We can write character data to the text files by using the following 2 methods.

write(str)

writelines(list of lines)

Eg:

1) f=open("abcd.txt",'w') 
2) f.write("Durga\n") 
3) f.write("Software\n") 
4) f.write("Solutions\n") 
5) print("Data written to the file successfully") 
6) f.close() 
abcd.txt:
Durga
Software
Solutions

Eg 2:

1) f=open("abcd.txt",'w') 
2) list=["sunny\n","bunny\n","vinny\n","chinny"] 
3) f.writelines(list) 
4) print("List of lines written to the file successfully") 
5) f.close() 
abcd.txt:
sunny
bunny
vinny
chinny

Reading Character Data from text files:

We can read character data from text file by using the following read methods.

read()=> To read total data from the file

read(n) => To read 'n' characters from the file

readline()=> To read only one line

readlines()=> To read all lines into a list

1) f=open("abc.txt",'r') 
2) data=f.read() 
3) print(data) 
4) f.close() 
5) 
6) Output
7) sunny 
8) bunny 
9) chinny 
10) vinny 

Eg 2: To read only first 10 characters:

1) f=open("abc.txt",'r') 
2) data=f.read(10) 
3) print(data) 
4) f.close() 
5) 
6) Output
7) sunny 
8) bunn 

Eg 3: To read data line by line:

1) f=open("abc.txt",'r') 
2) line1=f.readline() 
3) print(line1,end='') 
4) line2=f.readline() 
5) print(line2,end='') 
6) line3=f.readline() 
7) print(line3,end='') 
8) f.close() 
9) 
10) Output
11) sunny 
12) bunny 
13) chinny 

Eg 4: To read all lines into list:

1) f=open("abc.txt",'r') 
2) lines=f.readlines() 
3) for line in lines: 
4) print(line,end='') 
5) f.close() 
6) 
7) Output
8) sunny 
9) bunny 
10) chinny 
11) vinny 

The with statement:

The with statement can be used while opening a file.We can use this to group file operation statements within a block.

The advantage of with statement is it will take care closing of file,after completing all operations automatically even in the case of exceptions also, and we are not required to close explicitly.

Eg:

1) with open("abc.txt","w") as f: 
2) f.write("Durga\n") 
3) f.write("Software\n") 
4) f.write("Solutions\n") 
5) print("Is File Closed: ",f.closed) 
6) print("Is File Closed: ",f.closed) 
7) 
8) Output
9) Is File Closed: False 
10) Is File Closed: True 

The seek() and tell() methods:

tell():

==>We can use tell() method to return current position of the cursor(file pointer) from beginning of the file. [ can you plese telll current cursor position]

The position(index) of first character in files is zero just like string index.

Eg:

1) f=open("abc.txt","r") 
2) print(f.tell()) 
3) print(f.read(2)) 
4) print(f.tell()) 
5) print(f.read(3)) 
6) print(f.tell()) 
abc.txt:
sunny
bunny
chinny
vinny
Output:
0
su
2
nny
5

seek():

We can use seek() method to move cursor(file pointer) to specified location.

[Can you please seek the cursor to a particular location]

f.seek(offset, fromwhere)

offset represents the number of positions

The allowed values for second attribute(from where) are

0---->From beginning of file(default value)

1---->From current position

2--->From end of the file

Eg:

1) data="All Students are STUPIDS" 
2) f=open("abc.txt","w") 
3) f.write(data) 
4) with open("abc.txt","r+") as f: 
5) text=f.read() 
6) print(text) 
7) print("The Current Cursor Position: ",f.tell()) 
8) f.seek(17) 
9) print("The Current Cursor Position: ",f.tell()) 
10) f.write("GEMS!!!") 
11) f.seek(0) 
12) text=f.read() 
13) print("Data After Modification:") 
14) print(text) 
15) 
16) Output
17) 
18) All Students are STUPIDS 
19) The Current Cursor Position: 24 
20) The Current Cursor Position: 17 
21) Data After Modification:
22) All Students are GEMS!!! 

Handling Binary Data:

It is very common requirement to read or write binary data like images,video files,audio files etc.

Q. Program to read image file and write to a new image file?

1) f1=open("rossum.jpg","rb") 
2) f2=open("newpic.jpg","wb") 
3) bytes=f1.read() 
4) f2.write(bytes) 
5) print("New Image is available with the name: newpic.jpg") 

Handling csv files:

CSV==>Comma seperated values

As the part of programming,it is very common requirement to write and read data wrt csv files. Python provides csv module to handle csv files.

Writing data to csv file:

1) import csv 
2) with open("emp.csv","w",newline='') as f: 
3) w=csv.writer(f) # returns csv writer object 
4) w.writerow(["ENO","ENAME","ESAL","EADDR"]) 
5) n=int(input("Enter Number of Employees:")) 
6) for i in range(n): 
7) eno=input("Enter Employee No:") 
8) ename=input("Enter Employee Name:") 
9) esal=input("Enter Employee Salary:") 
10) eaddr=input("Enter Employee Address:") 
11) w.writerow([eno,ename,esal,eaddr]) 
12) print("Total Employees data written to csv file successfully")

Zipping and Unzipping Files:

It is very common requirement to zip and unzip files.

The main advantages are:

  1. To improve memory utilization
  2. We can reduce transport time
  3. We can improve performance.

To perform zip and unzip operations, Python contains one in-bulit module zip file.

This module contains a class : ZipFile

To create Zip file:

We have to create ZipFile class object with name of the zip file,mode and constant ZIP_DEFLATED. This constant represents we are creating zip file.

f = ZipFile("files.zip","w","ZIP_DEFLATED")

Once we create ZipFile object,we can add files by using write() method.

f.write(filename)

Eg:

1) from zipfile import * 
2) f=ZipFile("files.zip",'w',ZIP_DEFLATED) 
3) f.write("file1.txt") 
4) f.write("file2.txt") 
5) f.write("file3.txt") 
6) f.close() 
7) print("files.zip file created successfully") 

To perform unzip operation:

We have to create ZipFile object as follows

f = ZipFile("files.zip","r",ZIP_STORED)

ZIP_STORED represents unzip operation. This is default value and hence we are not 

required to specify.

Once we created ZipFile object for unzip operation,we can get all file names present in 

that zip file by using namelist() method.

names = f.namelist()

Eg:

1) from zipfile import * 
2) f=ZipFile("files.zip",'r',ZIP_STORED) 
3) names=f.namelist() 
4) for name in names: 
5) print( "File Name: ",name) 
6) print("The Content of this file is:") 
7) f1=open(name,'r') 
8) print(f1.read()) 
9) print() 

Working with Directories:

It is very common requirement to perform operations for directories like

  1. To know current working directory
  2. To create a new directory
  3. To remove an existing directory
  4. To rename a directory
  5. To list contents of the directory

etc...

To perform these operations,Python provides inbuilt module os,which contains several functions to perform directory related operations.

Q1. To Know Current Working Directory:

import os
cwd=os.getcwd()
print("Current Working Directory:",cwd)

Q2. To create a sub directory in the current working directory:

import os
os.mkdir("mysub")
print("mysub directory created in cwd")

Q3. To create a sub directory in mysub directory:

 cwd
    |-mysub
        |-mysub2
import os
os.mkdir("mysub/mysub2")
print("mysub2 created inside mysub")

Q4. To create multiple directories like sub1 in that sub2 in that sub3:

import os
os.makedirs("sub1/sub2/sub3")
print("sub1 and in that sub2 and in that sub3 directories created")

Q5. To remove a directory:

import os
os.rmdir("mysub/mysub2")
print("mysub2 directory deleted")

Q6. To remove multiple directories in the path:

import os
os.removedirs("sub1/sub2/sub3")
print("All 3 directories sub1,sub2 and sub3 removed")

Q7. To rename a directory:

import os
os.rename("mysub","newdir")
print("mysub directory renamed to newdir")

Q. What is the difference between listdir() and walk() functions?

In the case of listdir(), we will get contents of specified directory but not sub directory 

contents. But in the case of walk() function we will get contents of specified directory and 

its sub directories also.

Running Other programs from Python program:

os module contains system() function to run programs and commands.

It is exactly same as system() function in C language.

os.system("commad string")

 The argument is any command which is executing from DOS.

 Eg:

import os
os.system("dir *.py")
os.system("py abc.py")

How to get information about a File:

We can get statistics of a file like size, last accessed time,last modified time etc by using 

stat() function of os module.

stats = os.stat("abc.txt")

The statistics of a file includes the following parameters:

st_mode==>Protection Bits
st_ino==>Inode number
st_dev===>device
st_nlink===>no of hard links
st_uid===>userid of owner
st_gid==>group id of owner
st_size===>size of file in bytes
st_atime==>Time of most recent access
st_mtime==>Time of Most recent modification
st_ctime==> Time of Most recent meta data change

Pickling and Unpickling of Objects:

Sometimes we have to write total state of object to the file and we have to read total object from the file.

The process of writing state of object to the file is called pickling and the process of reading state of an object from the file is called unpickling. 

We can implement pickling and unpickling by using pickle module of Python.

pickle module contains dump() function to perform pickling.

pickle.dump(object,file)

pickle module contains load() function to perform unpickling

obj=pickle.load(file)


Writing and Reading State of object by using pickle Module:

1) import pickle 
2) class Employee: 
3)  def __init__(self,eno,ename,esal,eaddr): 
4)      self.eno=eno; 
5)      self.ename=ename; 
6)      self.esal=esal; 
7)      self.eaddr=eaddr; 
8)  def display(self): 
9)      print(self.eno,"\t",self.ename,"\t",self.esal,"\t",self.eaddr) 
10) with open("emp.dat","wb") as f: 
11)     e=Employee(100,"Durga",1000,"Hyd") 
12)     pickle.dump(e,f) 
13)     print("Pickling of Employee Object completed...") 
14) 
15) with open("emp.dat","rb") as f: 
16)     obj=pickle.load(f) 
17)     print("Printing Employee Information after unpickling") 
18)     obj.display() 

Writing Multiple Employee Objects to the file:

emp.py:

1) class Employee: 
2)  def __init__(self,eno,ename,esal,eaddr): 
3)      self.eno=eno; 
4)      self.ename=ename; 
5)      self.esal=esal; 
6)      self.eaddr=eaddr; 
7) def display(self): 
8) 
9) print(self.eno,"\t",self.ename,"\t",self.esal,"\t",self.eaddr) 

pick.py:

1) import emp,pickle 
2) f=open("emp.dat","wb") 
3) n=int(input("Enter The number of Employees:")) 
4) for i in range(n): 
5)      eno=int(input("Enter Employee Number:")) 
6)      ename=input("Enter Employee Name:") 
7)      esal=float(input("Enter Employee Salary:")) 
8)      eaddr=input("Enter Employee Address:") 
9)      e=emp.Employee(eno,ename,esal,eaddr) 
10)     pickle.dump(e,f) 
11) print("Employee Objects pickled successfully") 

"Python File Handling"

"Python File Handling Exercises"

"Python File Handling Programs"

"Python File Handling Questions"

"Python File Handling Examples"

"Python File Handling Functions"

"Python Csv File Handling"

"Python Binary File Handling"

"Python Excel File Handling"

"Python Data File Handling"