0% found this document useful (0 votes)
7 views3 pages

Python Assignment 3

The document contains a Python programming assignment with five questions focused on file handling, path classification, shelf files, moving files, and ZIP file operations. Each question includes code snippets and their corresponding outputs, demonstrating various functionalities such as checking path types, appending and merging file contents, manipulating shelf files, moving files between directories, and extracting ZIP files. The assignment showcases practical applications of Python for file management tasks.

Uploaded by

jiyachawan73
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Python Assignment 3

The document contains a Python programming assignment with five questions focused on file handling, path classification, shelf files, moving files, and ZIP file operations. Each question includes code snippets and their corresponding outputs, demonstrating various functionalities such as checking path types, appending and merging file contents, manipulating shelf files, moving files between directories, and extracting ZIP files. The assignment showcases practical applications of Python for file management tasks.

Uploaded by

jiyachawan73
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like