1.
Develop a prg to sort the contents of the text file and write sorted contents to a separate
text file
Program:
namelist = open('[Link]')
names = [Link]()
[Link]()
names = [[Link]() for name in names]
[Link]()
sorted_file = open('sorted_namelist.txt', 'w')
for name in names:
sorted_file.write(name + '\n')
sorted_file.close()
2. Use string methods like strip, length. List methods, sort method and file methods Open,
readlines, write.
strip(): Removes whitespace from beginning and end.
len(): Returns the number of characters in a string.
sort(): Sorts list in ascending order.
open(): Opens a file.
readlines(): Reads lines into a list.
write(): Writes content to a file.
Example:
lines = open('[Link]').readlines()
lines = [[Link]() for line in lines]
[Link]()
with open('sorted_data.txt', 'w') as f:
[Link]([line + '\n' for line in lines])
3. Explain moving and renaming files in shutil module and deleting in shutil module.
[Link](): Moves or renames a file or directory.
[Link](): Deletes a file.
Example:
import shutil, os
[Link]('[Link]', 'new_folder/[Link]') # Move & Rename
[Link]('new_folder/[Link]') # Delete
4. What is meant by compressed files. Explain reading, extracting, and creating zip files.
Compressed files reduce file size.
Use 'zipfile' module.
Example:
import zipfile
# Creating zip
with [Link]('[Link]', 'w') as zipf:
[Link]('[Link]')
# Extracting zip
with [Link]('[Link]', 'r') as zipf:
[Link]('extracted')
# Reading contents
with [Link]('[Link]', 'r') as zipf:
print([Link]())
5. Explain concepts of organizing files using shutil module with suitable examples
shutil helps automate file operations.
Example:
import shutil
[Link]('[Link]', 'backup/[Link]') # Copy file
[Link]('backup/[Link]', 'archive/report_backup.txt') # Move file
6. Explain with examples topic of copying and pasting strings with pyperclipmodule
pyperclip module is used to copy/paste text.
Example:
import pyperclip
[Link]("Hello")
text = [Link]()
print(text)
7. Define absolute and relative path with examples
Absolute Path: Complete path from root (e.g. C:\Users\Rishav\[Link])
Relative Path: Path relative to current directory (e.g. folder\[Link])
8. Difference between os and [Link] module. Discuss 4 methods: chdir, walk(), listdir(),
getcwd()
os handles system operations. [Link] handles file paths.
[Link](): Changes current directory.
[Link](): Lists files in directory.
[Link](): Returns current working directory.
[Link](): Yields root, dirs, files.
Example:
import os
[Link]('C:\NewFolder')
print([Link]())
print([Link]())
for root, dirs, files in [Link]('.'):
print(root, dirs, files)
9. Describe reading and writing files in python with suitable examples
Read:
with open('[Link]', 'r') as f:
data = [Link]()
Write:
with open('[Link]', 'w') as f:
[Link]("New content")
10. How do we specify and handle absolute relative path
Use [Link] functions.
Example:
import os
absolute = [Link]('[Link]')
print("Absolute:", absolute)
relative = [Link]('C:\Users\[Link]', 'C:\Users')
print("Relative:", relative)
11. Explain concepts of startswith and endswith string methods and join and split with
examples
startswith(): Checks start of string.
endswith(): Checks end of string.
join(): Joins list into string.
split(): Splits string into list.
Example:
text = "Hello World"
print([Link]("Hello")) # True
print([Link]("World")) # True
words = ['Python', 'is', 'fun']
print(' '.join(words))
sentence = "Learn Python"
print([Link]())
12. Explain following string methods with example: join, islower, strip, center
join(): Joins list to string.
islower(): Checks if string is lowercase.
strip(): Removes whitespace.
center(): Centers string.
Example:
s = 'python'
print([Link]())
items = ['one', 'two']
print('-'.join(items))
text = ' hello '
print([Link]())
print('hi'.center(10, '*'))