Day 26: Exploring File Handling Concepts in Python

Day 26: Exploring File Handling Concepts in Python

Introduction:

Hello fellow coders! Today, we will discuss something more on Zipping and Unzipping files in my Python journey, and I'm excited to share my experiences with you. In this series, I'll document my progress, successes, and setbacks. Join me as I explore the world of Python!

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.

Syntax :

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

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

f.write(filename)

Example:

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

Syntax :

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()

from zipfile import *   
f=ZipFile("files.zip",'r',ZIP_STORED)  
print (f) 
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

#To print all statistics of file abc.txt

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

#To print specified properties: 
'''
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))

Challenges :

  1. Understanding Zipping and Unzipping files .

  2. Handling errors.

Resources :

  1. Official Python Documentation: File handling.

  2. W3Schools' Python Tutorial: File handling.

  3. Scaler's Python Course: File handling.

Goals for Tomorrow :

  1. Explore something more on exception handling.

Conclusion :

Day 26’s a success!

What are your favorite Python resources? Share in the comments below.

Connect with me :

GitHub: [ github.com/p-archana1 ]

LinkedIn : [ linkedin.com/in/archana-prusty-4aa0b827a ]

Join the conversation :

Share your own learning experiences or ask questions in the comments.

HAPPY LEARNING!!

THANK YOU!!