0% found this document useful (0 votes)
3 views6 pages

Python_QA_Concepts

The document provides a comprehensive overview of various Python programming concepts, including file handling, string manipulation, and the use of modules like shutil and pyperclip. It covers sorting file contents, moving and renaming files, working with compressed files, and understanding absolute and relative paths. Additionally, it explains string methods and provides examples for each topic to illustrate their usage.

Uploaded by

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

Python_QA_Concepts

The document provides a comprehensive overview of various Python programming concepts, including file handling, string manipulation, and the use of modules like shutil and pyperclip. It covers sorting file contents, moving and renaming files, working with compressed files, and understanding absolute and relative paths. Additionally, it explains string methods and provides examples for each topic to illustrate their usage.

Uploaded by

A K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Develop a prg to sort the contents of the text file and write sorted contents to a separate

text file

Program:

namelist = open('namelist.txt')

names = namelist.readlines()

namelist.close()

names = [name.strip() for name in names]

names.sort()

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('data.txt').readlines()

lines = [line.strip() for line in lines]


lines.sort()

with open('sorted_data.txt', 'w') as f:

f.writelines([line + '\n' for line in lines])

3. Explain moving and renaming files in shutil module and deleting in shutil module.

shutil.move(): Moves or renames a file or directory.

shutil.os.remove(): Deletes a file.

Example:

import shutil, os

shutil.move('old.txt', 'new_folder/new.txt') # Move & Rename

os.remove('new_folder/new.txt') # 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 zipfile.ZipFile('archive.zip', 'w') as zipf:

zipf.write('file1.txt')

# Extracting zip

with zipfile.ZipFile('archive.zip', 'r') as zipf:

zipf.extractall('extracted')
# Reading contents

with zipfile.ZipFile('archive.zip', 'r') as zipf:

print(zipf.namelist())

5. Explain concepts of organizing files using shutil module with suitable examples

shutil helps automate file operations.

Example:

import shutil

shutil.copy('report.txt', 'backup/report.txt') # Copy file

shutil.move('backup/report.txt', '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

pyperclip.copy("Hello")

text = pyperclip.paste()

print(text)

7. Define absolute and relative path with examples

Absolute Path: Complete path from root (e.g. C:\Users\Rishav\file.txt)

Relative Path: Path relative to current directory (e.g. folder\file.txt)

8. Difference between os and os.path module. Discuss 4 methods: chdir, walk(), listdir(),

getcwd()

os handles system operations. os.path handles file paths.


os.chdir(): Changes current directory.

os.listdir(): Lists files in directory.

os.getcwd(): Returns current working directory.

os.walk(): Yields root, dirs, files.

Example:

import os

os.chdir('C:\NewFolder')

print(os.listdir())

print(os.getcwd())

for root, dirs, files in os.walk('.'):

print(root, dirs, files)

9. Describe reading and writing files in python with suitable examples

Read:

with open('file.txt', 'r') as f:

data = f.read()

Write:

with open('file.txt', 'w') as f:

f.write("New content")

10. How do we specify and handle absolute relative path

Use os.path functions.

Example:

import os
absolute = os.path.abspath('file.txt')

print("Absolute:", absolute)

relative = os.path.relpath('C:\Users\file.txt', '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(text.startswith("Hello")) # True

print(text.endswith("World")) # True

words = ['Python', 'is', 'fun']

print(' '.join(words))

sentence = "Learn Python"

print(sentence.split())

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(s.islower())

items = ['one', 'two']

print('-'.join(items))

text = ' hello '

print(text.strip())

print('hi'.center(10, '*'))

You might also like