Day 25 File Handling: Learn More Techniques Today

Day 25 File Handling: Learn More Techniques Today

Introduction:

Hello fellow coders! Today, we will discuss something more on Binary file handling and working with Directory 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!

Handling Binary Data:

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

#Program to read image file and write to a new image file?

f1=open("download.jfif","rb")
f2=open("new.JPG","wb")
bytes=f1.read()
f2.write(bytes)
print("New Image is available with the name: newpic.jpg")

Handling csv files:

CSV => Comma separated values

As the part of programming, it is very common requirement to write and read data write csv files.

Python provides csv module to handle csv files.

Writing data to csv file:

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

Reading Data from csv file:

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

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...

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

Note:

Assume mysub already present in cwd.

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

Q8. To know contents of directory:

os module provides listdir() to list out the contents of the specified directory.

It won't display the contents of sub directory.

Example:

import os
print(os.listdir("."))

Challenges :

  1. Understanding various types of binary File handling functions , and working with directory.

  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 File handling.

Conclusion :

Day 25’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!!