IKH

Different file format

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?

Python
f1=open("rossum.jpg","rb")
f2=open("newpic.jpg","wb")
bytes=f1.read()
f2.write(bytes)
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

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

Note

  • Observe the difference with newline attribute and without
  • with open(“emp.csv”,”w”,newline=”) as f:
  • with open(“emp.csv”,”w”) as f:

Note

  • If we are not using newline attribute then in the csv file blank lines will be included between data. To prevent these blank lines, newline attribute is required in Python-3,but in Python-2 just we can specify mode as ‘wb’ and we are not required to use newline attribute.

Reading Data from csv file

Python
import csv
f=open("emp.csv",'r')
r=csv.reader(f) #returns csv reader object
data=list(r)
#print(data)
for line in data:
for word in line:
print(word,"\t",end='')
print()

Output
D:\Python_classes>py test.py
ENO ENAME ESAL EADDR
100 Durga 1000 Hyd
200 Sachin 2000 Mumbai
300 Dhoni 3000 Ranchi

Zipping and Unzipping Files

  • It is very common requirement to zip and unzip files.
  • The main advantages are:
  • To improve memory utilization
  • We can reduce transport time
  • 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)

Example

Python
from zipfile import *
f=ZipFile("files.zip",'w',ZIP_DEFLATED)
f.write("file1.txt")
f.write("file2.txt")
f.write("file3.txt")
f.close()
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()

Example

Python
from zipfile import *
f=ZipFile("files.zip",'r',ZIP_STORED)
names=f.namelist()
for name in names:
print( "File Name: ",name)
print("The Content of this file is:")
f1=open(name,'r')
print(f1.read())
print() 

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

Note

  • st_atime, st_mtime and st_ctime returns the time as number of milli seconds since Jan 1st
  • 1970 ,12:00AM. By using datetime module fromtimestamp() function,we can get exact date and time.

Q. To print all statistics of file abc.txt

Python
import os
stats=os.stat("abc.txt")
print(stats)

Output
os.stat_result(st_mode=33206, st_ino=844424930132788, st_dev=2657980798, st_nlin
k=1, st_uid=0, st_gid=0, st_size=22410, st_atime=1505451446, st_mtime=1505538999
, st_ctime=1505451446) 

Q. To print specified properties

Python
import os
from datetime import *
stats=os.stat("abc.txt")
print("File Size in Bytes:",stats.st_size)
print("File Last Accessed Time:",datetime.fromtimestamp(stats.st_atime))
print("File Last Modified Time:",datetime.fromtimestamp(stats.st_mtime))
Output
File Size in Bytes: 22410
File Last Accessed Time: 2017-09-15 10:27:26.599490
File Last Modified Time: 2017-09-16 10:46:39.245394

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

Python
import pickle
class Employee:
def __init__(self,eno,ename,esal,eaddr):
self.eno=eno;
self.ename=ename;
self.esal=esal;
self.eaddr=eaddr;
def display(self):
print(self.eno,"\t",self.ename,"\t",self.esal,"\t",self.eaddr)
with open("emp.dat","wb") as f:
e=Employee(100,"Durga",1000,"Hyd")
pickle.dump(e,f)
print("Pickling of Employee Object completed...")

with open("emp.dat","rb") as f:
obj=pickle.load(f)
print("Printing Employee Information after unpickling")
obj.display()

Writing Multiple Employee Objects to the file

emp.py

Python
class Employee:
def __init__(self,eno,ename,esal,eaddr):
self.eno=eno;
self.ename=ename;
self.esal=esal;
self.eaddr=eaddr;
def display(self):

print(self.eno,"\t",self.ename,"\t",self.esal,"\t",self.eaddr)

pick.py

Python
import emp,pickle
f=open("emp.dat","wb")
n=int(input("Enter The number of Employees:"))
for i in range(n):
eno=int(input("Enter Employee Number:"))
ename=input("Enter Employee Name:")
esal=float(input("Enter Employee Salary:"))
eaddr=input("Enter Employee Address:")
e=emp.Employee(eno,ename,esal,eaddr)
pickle.dump(e,f)
print("Employee Objects pickled successfully")

unpick.py

Python
import emp,pickle
f=open("emp.dat","rb")
print("Employee Details:")
while True:
try:
obj=pickle.load(f)
obj.display()
except EOFError:
print("All employees Completed")
break
f.close() 

Report an error