Assignments 4 FILE in Python Programming BCC402
Assignments 4 FILE in Python Programming BCC402
[Set – 1]
1. Write a program that writes 10 random numbers to a file 'numbers.txt'. Each random number
should be in the range of 1 through 100. Solution
2. Write a program that reads and display all of the numbers stored in the file numbers.txt
(created in question 1) and calculates their total. Solution
3. Write a function, digit_count() in Python that counts and displays the number of digits in the
).
text file named 'sample.txt'. For example, if the content of 'sample.txt' is as follows :
02
The team achieved a milestone in 2023. They completed a multi-million-dollar project ahead of
C4
schedule. Stakeholders were impressed with a 98% success rate.
C
(B
4. Write a function lines_count() that reads lines from a text file named 'zen.txt' and displays the
lines that begin with any vowel. Assume the file contains the following text and already exists on
the computer's disk:
Solution
5. Assume that the file 'notes.txt' containing some text and exists on the computer’s disk. Write
Pr
a program that display only those words from 'notes.txt' file whose length is more than seven.
Keep in mind that any punctuation marks at the beginning or end of a word should also be
on
6. Write a function last_digit_words() in Python to count the words ending with a digit in a text
file "notes.txt". For example, if the file content is as follows :
Py
The Computer6 hums softly as I sit with a Book3 in hand, diving into a world of imagination.
Outside, my friends gather at House9 and I quickly grab my Pen2 to jot down the address.
7. Assume that a file 'names.txt' containing a series of names (as strings) exists on the
computer’s disk. Write a function, first_five() that displays only the first five lines of the file’s
contents. If the file contains less than five lines, it should display the file’s entire contents.
Solution
8. Write a Python program that reads a text file and prints its contents in reverse order (from the
last line to the first line). Solution
9. Write the definition of a Python function named long_lines( ) which reads the contents of a
text file named 'lines.txt' and displays those lines from the file which have at least 8 words in it.
For example, if the content of 'lines.txt' is as follows :
).
Special cases aren't special enough to break the rules.
02
The output should be:
C4
Special cases aren't special enough to break the rules. Solution
10. Assume that a file named 'feedback.txt' contains student feedback in the following format:
C
Positive: Saksham improved grades, more confident now.
(B
Negative: Arav needs better time management for coursework.
Negative: Samar should work on communication in group activities.
g_
Negative: Soham could benefit from asking more questions in class.
Positive: Sakshi excels academically, a great team player.
in
Write a Python function named feedback_analysis() to calculate and display the following
m
information:
m
11. Create a Python function make_copy() that reads a text file 'input.txt' and writes its contents
to a new file 'output.txt', capitalizing the first letter of each word. For example, if 'input.txt'
Pr
"In the world of programming, there are no limits to what you can achieve. Aim high!"
on
Solution
Py
File Handling
[Set – 2]
1. Write a function in python to read the content from a text file "poem.txt" line by line and
display the same on screen. Solution
2. Write a function in python to count the number of lines from a text file "story.txt" which is not
starting with an alphabet "T".
Example: If the file "story.txt" contains the following lines: A boy is playing there.
).
There is a playground.
02
An aeroplane is in the sky.
The sky is pink.
C4
Alphabets and numbers are allowed in the password.
C
3. Write a function in Python to count and display the total number of words in a text file.
(B
Solution
g_
4. Write a function in Python to read lines from a text file "notes.txt". Your function should find
and display the occurrence of the word "the".
in
For example: If the content of the file is:
m
"India is the fastest-growing economy. India is looking for more investments around the globe.
The whole world is looking at India as a great market. Most of the Indians can foresee the
m
5. Write a function display_words() in python to read lines from a text file "story.txt", and display
those words, which are less than 4 characters. Solution
Pr
6. Write a function in Python to count the words "this" and "these" present in a text file
"article.txt". [Note that the words "this" and "these" are complete words] Solution
on
7. Write a function in Python to count words in a text file those are ending with alphabet "e".
th
Solution
Py
9. A text file named "matter.txt" contains some text, which needs to be displayed such that every
next character is separated by a symbol "#". Write a function definition for hash_display() in
Python that would display the entire content of the file matter.txt in the desired format.
Example :
If the file matter.txt has the following content stored in it :
THE WORLD IS ROUND
add_employee()
This function should prompt the user to input data for an employee, including Employee ID,
Name, Department, and Salary. The data should be stored in a dictionary format: {'ID': ID, 'Name':
).
Name, 'Department': Department, 'Salary': Salary}. The function should then add the dictionary
02
object to the emp.dat binary file using pickling.
C4
display_records()
This function should display all the records of employees stored in emp.dat file on the screen.
Each employee's details should be displayed with their corresponding attributes: Employee ID,
C
Name, Department, and Salary.
(B
search_employee(id)
g_
This function should receive an Employee ID as an argument. It should search through the
emp.dat file and display the details of the employee with the given ID. If no employee with the
provided ID is found, display an appropriate message.
in
m
In the program, include a menu-based interface that allows users to choose which function to
execute.
m
ra
Sample Output
og
Solution
Pr
2. Write a Python program that manages book records using a binary file called book.dat. The
program should have the following functions:
on
add_record()
This function should prompt the user to input data for a book, including the Book Number, Book
th
Name, Author, and Price. The data should be stored in a list format: [BookNo, Book_Name,
Py
Author, Price]. The function should then add the book data to the book.dat binary file using
pickling.
display_records()
This function should display all the records stored in the book.dat file on the screen. Each record
should be displayed with its corresponding attributes: Book Number, Book Name, Author, and
Price.
books_by_author()
This function should ask the user to input an author's name. It should then search through the
records in the book.dat file and display all the records for books written by the given author.
books_by_price(price)
This function should receive a maximum price as an argument. It should search through the
records in the book.dat file and display all the records for books with a price less than the
provided maximum price.
copy_data()
This function should read the contents of the book.dat file. It should copy the records of books
whose price is more than $500 to a new file named costly_book.dat using pickling. The function
should return the count of records copied.
).
delete_record(book_number)
02
This function should receive book number as an argument.. If a record with the given book
number exists, it should be deleted from the book.dat file. If the record does not exist, display an
C4
error message.
C
update_record(book_number)
This function should receive a maximum price as an argument. If a record with the given book
(B
number exists, prompt the user to update the Book Name, Author, and Price for that record. If
the record does not exist, display an error message.
g_
In the program, include a menu-based interface that allows users to choose which function to
in
execute.
m
Sample Output
m
Solution
ra
og
3. Aditi has used a text editing software to type some text. After saving the article as
WORDS.TXT, she realised that she has wrongly typed alphabet J in place of alphabet I
Pr
Write a function definition for JTOI() in Python that would display the corrected version of entire
on
content of the file WORDS.TXT with all the alphabets "J" to be displayed as an alphabet "I" on
screen.
th
Note: Assuming that WORD.TXT does not contain any J alphabet otherwise.
Py
Example:
If Aditi has stored the following content in the file WORDS.TXT:
WELL, THJS JS A WORD BY JTSELF. YOU COULD STRETCH THJS TO BE A SENTENCE
The function JTOI() should display the following content:
WELL, THIS IS A WORD BY ITSELF. YOU COULD STRETCH THIS TO BE A SENTENCE Solution
Py
th
on
Pr
og
ra
m
m
in
g_
(B
C C4
02
).