Programming in Python - Assignment 3
(With Output)
Q1. classify_path Function
Code:
import os
def classify_path(path):
if [Link](path):
return "Absolute path"
elif [Link](path):
return "Directory"
elif [Link](path):
return "File"
else:
return "Invalid path"
path = "C:/Windows"
print(classify_path(path))
Output:
Absolute path
Q2. File Handling
Append Code:
with open("[Link]", "a") as f:
[Link]("Hello World\n")
Output:
Data appended successfully (file content updated)
Merge Code:
with open("[Link]", "r") as f1, open("[Link]", "a") as f2:
[Link]([Link]())
Output:
Contents of file1 added to file2
Q3. Shelf File
import shelve
s = [Link]("data")
s["k1"] = "A"
s["k2"] = "B"
s["k3"] = "C"
print(list([Link]()))
s["k2"] = "Updated"
del s["k3"]
print(dict(s))
[Link]()
Output:
['k1', 'k2', 'k3']
{'k1': 'A', 'k2': 'Updated'}
Q4. Move Files
import os, shutil
def move_files(src, dest):
for file in [Link](src):
[Link]([Link](src, file), dest)
[Link](src)
move_files("src_folder", "dest_folder")
Output:
Files moved successfully and source folder deleted
Q5. ZIP File
import zipfile
with [Link]("[Link]", 'r') as z:
print([Link]())
[Link]("output")
Output:
['[Link]', '[Link]']
Files extracted successfully