Python_QA_Concepts
Python_QA_Concepts
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.sort()
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.
Example:
lines = open('data.txt').readlines()
3. Explain moving and renaming files in shutil module and deleting in shutil module.
Example:
import shutil, os
os.remove('new_folder/new.txt') # Delete
4. What is meant by compressed files. Explain reading, extracting, and creating zip files.
Example:
import zipfile
# Creating zip
zipf.write('file1.txt')
# Extracting zip
zipf.extractall('extracted')
# Reading contents
print(zipf.namelist())
5. Explain concepts of organizing files using shutil module with suitable examples
Example:
import shutil
6. Explain with examples topic of copying and pasting strings with pyperclipmodule
Example:
import pyperclip
pyperclip.copy("Hello")
text = pyperclip.paste()
print(text)
8. Difference between os and os.path module. Discuss 4 methods: chdir, walk(), listdir(),
getcwd()
Example:
import os
os.chdir('C:\NewFolder')
print(os.listdir())
print(os.getcwd())
Read:
data = f.read()
Write:
f.write("New content")
Example:
import os
absolute = os.path.abspath('file.txt')
print("Absolute:", absolute)
print("Relative:", relative)
11. Explain concepts of startswith and endswith string methods and join and split with
examples
Example:
print(text.startswith("Hello")) # True
print(text.endswith("World")) # True
print(' '.join(words))
print(sentence.split())
12. Explain following string methods with example: join, islower, strip, center
Example:
s = 'python'
print(s.islower())
print('-'.join(items))
print(text.strip())
print('hi'.center(10, '*'))