0% found this document useful (0 votes)
12 views4 pages

Python Lab Assignment 5

This document outlines a Python lab assignment focused on file reading and writing techniques. It includes examples for reading lines from a file, appending text, counting words and characters, writing to CSV, comparing files, and manipulating file content. Each section provides code snippets demonstrating the respective file operations.

Uploaded by

kudesiat
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)
12 views4 pages

Python Lab Assignment 5

This document outlines a Python lab assignment focused on file reading and writing techniques. It includes examples for reading lines from a file, appending text, counting words and characters, writing to CSV, comparing files, and manipulating file content. Each section provides code snippets demonstrating the respective file operations.

Uploaded by

kudesiat
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/ 4

Python Lab Assignment 5

Topic: File Reading and Writing

1. Read and Print Each Line of a File

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

line = file.readline()

while line:

print(line.strip())

line = file.readline()

2. Append a Line to an Existing File

with open('example.txt', 'a') as file:

file.write('This is a new line.\n')

3. Read First 10 Characters, Use seek(5), and Read Next 5 Characters

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

print('First 10 characters:', file.read(10))

file.seek(5)

print('Next 5 characters from position 6:', file.read(5))

4. Count Words, Lines, and Characters in a File

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

content = file.read()

num_lines = len(content.split('\n'))

num_words = len(content.split())

num_chars = len(content)

print('Lines:', num_lines)

print('Words:', num_words)
print('Characters:', num_chars)

5. Append User Input to a File Until 'STOP' is Entered

with open('example.txt', 'a') as file:

while True:

user_input = input('Enter text (type STOP to end): ')

if user_input == 'STOP':

break

file.write(user_input + '\n')

6. Write a List of Dictionaries to a CSV File

import csv

students = [

{'Name': 'Alice', 'Age': 20, 'Grade': 'A'},

{'Name': 'Bob', 'Age': 22, 'Grade': 'B'},

{'Name': 'Charlie', 'Age': 21, 'Grade': 'A+'}

with open('students.csv', 'w', newline='') as file:

fieldnames = ['Name', 'Age', 'Grade']

writer = csv.DictWriter(file, fieldnames=fieldnames)

writer.writeheader()

writer.writerows(students)

7. Compare Two Files Line by Line and Report Differences

with open('file1.txt', 'r') as file1, open('file2.txt', 'r') as file2:

for i, (line1, line2) in enumerate(zip(file1, file2), start=1):


if line1 != line2:

print(f'Difference at line {i}:')

print(f'File1: {line1.strip()}')

print(f'File2: {line2.strip()}')

8. Write File Lines in Reverse Order to a New File

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

lines = file.readlines()

with open('reversed_example.txt', 'w') as new_file:

for line in reversed(lines):

new_file.write(line)

9. Replace All Occurrences of a Word in a File

word_to_replace = 'oldword'

replacement_word = 'newword'

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

content = file.read()

content = content.replace(word_to_replace, replacement_word)

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

file.write(content)

10. Count Occurrences of a Specific Word in a File

word_to_count = 'example'
with open('example.txt', 'r') as file:

content = file.read()

count = content.count(word_to_count)

print(f'The word "{word_to_count}" appears {count} times in the file.')

You might also like