Project File Part 3
Project File Part 3
Page 41: Count and Display the Number of 5-Letter Words in a Text File
python Copy code
# Function to count and display the number of 5-letter words in a file def
count_five_letter_words(): with open('Sample.txt', 'r') as file: words =
file.read().split() five_letter_words = [word for word in words if len(word) == 5]
return len(five_letter_words) # Example usage: print(f"Number of 5-letter words:
{count_five_letter_words()}")
Sample Output:
Page 42: Display Lines that Start with the Letter 'G' from a Text File
# Function to display lines starting with 'G' from a file def lines_starting_with_g():
with open('MyNotes.txt', 'r') as file: lines = file.readlines() for line in lines: if
line.startswith('G'): print(line.strip()) # Example usage: lines_starting_with_g()
Sample Output:
Page 43: Display Words with Two Characters from a Text File
https://chatgpt.com 1/9
1/12/25, 10:14 PM ChatGPT
Sample Output:
Sample Output:
Page 45: Count Lines Starting with 'E' and 'T' in a Text File
# Function to count lines starting with 'E' or 'T' in a file def count_lines_et():
with open('REPORT.TXT', 'r') as file: lines = file.readlines() e_count = 0 t_count = 0
for line in lines: if line.startswith('E'): e_count += 1 elif line.startswith('T'):
t_count += 1 return e_count, t_count # Example usage: e_count, t_count =
count_lines_et() print(f"Lines starting with 'E': {e_count}, Lines starting with 'T':
{t_count}")
Sample Output:
csharp
https://chatgpt.com 2/9
1/12/25, 10:14 PM ChatGPT
Copy code
Lines starting with 'E': 3, Lines starting with 'T': 2
# Function to display lines containing 'TO' or 'DO' in a file def show_todo(): with
open('ABC.TXT', 'r') as file: lines = file.readlines() for line in lines: if 'TO' in
line or 'DO' in line: print(line.strip()) # Example usage: show_todo()
Sample Output:
Sample Output:
Page 48: Count Occurrences of 'A' and 'M' (Including Lowercase) in a Text File
python
https://chatgpt.com 3/9
1/12/25, 10:14 PM ChatGPT
Copy code
# Function to count occurrences of 'A' and 'M' in a file def am_count(): with
open('STORY.TXT', 'r') as file: content = file.read().lower() a_count =
content.count('a') m_count = content.count('m') return a_count + m_count # Example
usage: print(f"Occurrences of 'A' and 'M': {am_count()}")
Sample Output:
Sample Output:
Page 50: Count the Number of Times "my" Occurs in a Text File
Sample Output:
https://chatgpt.com 4/9
1/12/25, 10:14 PM ChatGPT
Occurrences of 'my': 12
Sample Output:
Sample Output:
Page 53: Display Lines Starting with 'P' from a Text File
https://chatgpt.com 5/9
1/12/25, 10:14 PM ChatGPT
Sample Output:
Sample Output:
Sample Output:
arduino
https://chatgpt.com 6/9
1/12/25, 10:14 PM ChatGPT
Copy code
Occurrences of 'The' or 'This': 10
Sample Output:
Vowel count: 56
Page 57: Copy Lines to a New File Excluding Lines Starting with '@'
# Function to copy lines from one file to another excluding lines starting with '@'
def filter_lines(oldfile, newfile): with open(oldfile, 'r') as infile: lines =
infile.readlines() with open(newfile, 'w') as outfile: for line in lines: if not
line.startswith('@'): outfile.write(line) # Example usage: filter_lines('source.txt',
'target.txt')
Sample Output:
No direct output; file is modified. The lines starting with '@' will be excluded in target.txt .
Page 58: Check File Name, Mode, Whether File is Closed or Not, and
Encoding Format
python
https://chatgpt.com 7/9
1/12/25, 10:14 PM ChatGPT
Copy code
# Function to check file details def check_file_details(filename): with open(filename,
'r') as file: print(f"File Name: {file.name}") print(f"File Mode: {file.mode}")
print(f"Is File Closed? {file.closed}") print(f"File Encoding: {file.encoding}") #
Example usage: check_file_details('source.txt')
Sample Output:
File Name: source.txt File Mode: r Is File Closed? False File Encoding: None
import pickle # Function to insert a student record into a binary file def
insert_rec(): with open('student.dat', 'ab') as file: rollno = int(input("Enter roll
number: ")) name = input("Enter student name: ") marks = float(input("Enter marks: "))
student = {'rollno': rollno, 'name': name, 'marks': marks} pickle.dump(student, file)
# Example usage: insert_rec()
import pickle # Function to search for a student record by roll number def
search_rollno(rollno): try: with open('student.dat', 'rb') as file: while True:
student = pickle.load(file) if student['rollno'] == rollno: print(f"Student Found:
{student}") return except EOFError: print('No record found.') # Example usage:
search_rollno(101)
import pickle # Function to add employee records to an existing binary file def
add_employee_records(): with open('emp.DAT', 'ab') as file: eid = int(input("Enter
Employee ID: ")) name = input("Enter Employee Name: ") designation = input("Enter
Designation: ") salary = float(input("Enter Salary: ")) employee = {'EID': eid,
'Ename': name, 'Designation': designation, 'Salary': salary} pickle.dump(employee,
file) # Example usage: add_employee_records()
https://chatgpt.com 9/9