0% found this document useful (0 votes)
17 views9 pages

Project File Part 3

Uploaded by

Sanju Kumari
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)
17 views9 pages

Project File Part 3

Uploaded by

Sanju Kumari
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/ 9

1/12/25, 10:14 PM ChatGPT

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:

javascript Copy code

Number of 5-letter words: 4

Page 42: Display Lines that Start with the Letter 'G' from a Text File

python Copy code

# 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:

sql Copy code

Good morning! Great day ahead!

Page 43: Display Words with Two Characters from a Text File

python Copy code

# Function to display words with exactly two characters in a file def


words_with_two_chars(): with open('POEM.txt', 'r') as file: content = file.read()
words = content.split() two_char_words = [word for word in words if len(word) == 2]

https://chatgpt.com 1/9
1/12/25, 10:14 PM ChatGPT

return two_char_words # Example usage: print(f"Words with two characters:


{words_with_two_chars()}")

Sample Output:

csharp Copy code

Words with two characters: ['is', 'of', 'to']

Page 44: Count Occurrences of "Catholic" or "Mother" in a File

python Copy code

# Function to count occurrences of "Catholic" or "mother" in a file def


count_catholic_mother(): with open('REPEATED.TXT', 'r') as file: content =
file.read().lower() catholic_count = content.count('catholic') mother_count =
content.count('mother') return catholic_count + mother_count # Example usage:
print(f"Occurrences of 'Catholic' or 'mother': {count_catholic_mother()}")

Sample Output:

arduino Copy code

Occurrences of 'Catholic' or 'mother': 5

Page 45: Count Lines Starting with 'E' and 'T' in a Text File

python Copy code

# 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

Page 46: Display Lines Containing 'TO' or 'DO' in a Text File

python Copy code

# 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:

vbnet Copy code

This is the way TO go. I DO believe in magic.

Page 47: Count the Number of "Me" or "My" in a Text File

python Copy code

# Function to count occurrences of "Me" or "My" in a file def count_me_my(): with


open('STORY.TXT', 'r') as file: content = file.read().lower() me_count =
content.count('me') my_count = content.count('my') return me_count + my_count #
Example usage: print(f"Occurrences of 'Me' or 'My': {count_me_my()}")

Sample Output:

arduino Copy code

Occurrences of 'Me' or 'My': 7

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:

arduino Copy code

Occurrences of 'A' and 'M': 50

Page 49: Display Lines Starting with 'H' in a Text File

python Copy code

# Function to display lines starting with 'H' in a file def lines_starting_with_h():


with open('para.txt', 'r') as file: lines = file.readlines() for line in lines: if
line.startswith('H'): print(line.strip()) # Example usage: lines_starting_with_h()

Sample Output:

sql Copy code

Hello, how are you? Have a great day ahead!

Page 50: Count the Number of Times "my" Occurs in a Text File

python Copy code

# Function to count occurrences of 'my' in a file def count_my(): with


open('Data.txt', 'r') as file: content = file.read().lower() return
content.count('my') # Example usage: print(f"Occurrences of 'my': {count_my()}")

Sample Output:

arduino Copy code

https://chatgpt.com 4/9
1/12/25, 10:14 PM ChatGPT

Occurrences of 'my': 12

Page 51: Count the Number of Lines in a Text File

python Copy code

# Function to count the number of lines in a file def count_lines_in_file(): with


open('abc.txt', 'r') as file: lines = file.readlines() return len(lines) # Example
usage: print(f"Number of lines in the file: {count_lines_in_file()}")

Sample Output:

javascript Copy code

Number of lines in the file: 20

Page 52: Count "The" or "This" in a Text File

python Copy code

# Function to count occurrences of "The" or "This" in a file def count_the_this():


with open('MY_TEXT_FILE.TXT', 'r') as file: content = file.read().lower() the_count =
content.count('the') this_count = content.count('this') return the_count + this_count
# Example usage: print(f"Occurrences of 'The' or 'This': {count_the_this()}")

Sample Output:

arduino Copy code

Occurrences of 'The' or 'This': 8

Page 53: Display Lines Starting with 'P' from a Text File

python Copy code

https://chatgpt.com 5/9
1/12/25, 10:14 PM ChatGPT

# Function to display lines starting with 'P' in a file def lines_starting_with_p():


with open('DIARY.TXT', 'r') as file: lines = file.readlines() for line in lines: if
line.startswith('P'): print(line.strip()) # Example usage: lines_starting_with_p()

Sample Output:

python Copy code

Pandas are wonderful creatures. Please pass the salt.

Page 54: Count Occurrences of "IS", "TO", or "UP" in a Text File

python Copy code

# Function to count occurrences of "IS", "TO", or "UP" in a file def isto_up_count():


with open('WRITER.TXT', 'r') as file: content = file.read().lower() is_count =
content.count('is') to_count = content.count('to') up_count = content.count('up')
return is_count + to_count + up_count # Example usage: print(f"Occurrences of 'IS',
'TO', or 'UP': {isto_up_count()}")

Sample Output:

arduino Copy code

Occurrences of 'IS', 'TO', or 'UP': 15

Page 55: Count Occurrences of "The" or "This" in a Text File


python Copy code

# Function to count occurrences of "The" or "This" in a file def


count_the_this_repeated(): with open('MY_TEXT_FILE.TXT', 'r') as file: content =
file.read().lower() the_count = content.count('the') this_count =
content.count('this') return the_count + this_count # Example usage:
print(f"Occurrences of 'The' or 'This': {count_the_this_repeated()}")

Sample Output:

arduino

https://chatgpt.com 6/9
1/12/25, 10:14 PM ChatGPT

Copy code
Occurrences of 'The' or 'This': 10

Page 56: Count Vowels in a Text File

python Copy code

# Function to count vowels in a file def vowel_count(): vowels = 'aeiou' with


open('MY_TEXT_FILE.TXT', 'r') as file: content = file.read().lower() vowel_count =
sum(1 for char in content if char in vowels) return vowel_count # Example usage:
print(f"Vowel count: {vowel_count()}")

Sample Output:

yaml Copy code

Vowel count: 56

Page 57: Copy Lines to a New File Excluding Lines Starting with '@'

python Copy code

# 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:

mathematica Copy code

File Name: source.txt File Mode: r Is File Closed? False File Encoding: None

Page 59: Binary File Operations for Student Records

(i) Insert a Record into a Binary File

python Copy code

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()

(ii) Search for a Student Record by Roll Number

python Copy code

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)

Page 60: Binary File Operations for Employee Records


https://chatgpt.com 8/9
1/12/25, 10:14 PM ChatGPT

(i) Add More Employee Records

python Copy code

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()

(ii) Show Employee Records with Designation "Salesman"

python Copy code

import pickle # Function to display employees with designation "Salesman" def


show_salesman(): try: with open('emp.DAT', 'rb') as file: while True: employee =
pickle.load(file) if employee['Designation'] == 'Salesman': print(f"Employee:
{employee}") except EOFError: print('End of file reached.') # Example usage:
show_salesman()

https://chatgpt.com 9/9

You might also like