Assignment-5 Text and Binary Files
Subject: Computer Science Workshop - 1 (CSE 2141)
Session: Sep 2025 to Jan 2026
Branch: Computer Science and Engineering (CSE)
Section: All
Course Outcome: CO1, CO2, CO3
Program Outcomes: PO1, PO2, PO3, and PO5
Learning Levels: Remembering (L1), Understanding (L2), Application (L3)
Q no. Questions Learning
Levels
Q1. Write a Python program to create a text file named [Link] and store the names of L1, L2
five students in it. After writing, open the file in read mode and display its contents on
the screen. Your program should ensure proper file handling (open, write, read, and
close). Use the with keyword for safer file operations.
Input:
["Alice", "Bob", "Charlie", "David", "Eva"]
Output:
File '[Link]' created successfully.
Contents of the file:
Alice
Bob
Charlie
David
Eva.
Q2. Write a Python program to demonstrate file exception handling. The program should L1, L2
attempt to open a file named [Link]. If the file does not exist, display an error
message like “Error: File not found.” instead of crashing. If the file exists, print its
contents.
Input:
Case 1: [Link] does not exist
Case 2: [Link] contains →
Python is powerful.
Output:
Case 1:
Error: File not found.
Case 2:
File opened successfully.
Contents of [Link]:
Python is powerful.
Q3. Write a program that uses the with keyword to handle files safely. The program L1, L2
should open a file called [Link], write a short poem into it, and then automatically
close the file without explicitly calling close(). After writing, reopen the file in read
mode and print the poem line by line.
Input:
Roses are red,
Violets are blue,
Python is fun,
And so are you.
Output:
Poem written to file successfully.
Reading back from [Link]:
Roses are red,
Violets are blue,
Python is fun,
And so are you.
Q4. Write a program to read a text file line by line and count: L1, L2
• The total number of lines.
• The total number of words.
• The total number of characters.
Print these counts as the output.
Input:
Python is great.
It makes coding simple.
Output:
Number of lines: 2
Number of words: 6
Number of characters: 38
Q5. Write a Python program that demonstrates the use of a file pointer (seek()). The L2, L3
program should:
• Open a text file,
• Read the first 10 characters,
• Move the file pointer back to the beginning,
• Read and print the entire file.
Input:
Hello Python World!
Output:
First 10 characters: Hello Pyth
Full file contents after seek(): Hello Python World!
Q6. (a) Write a Python code that asks the user to enter a diary note and then attempts to L2, L3
create a text file named "[Link]". Before writing, the program must check
whether the file already exists; if it does, display a message informing the user that
the file already exists and avoid overwriting any previous content. If the file does
not exist, the program should create it and write the current date followed by the
user's diary entry.
(b) In a second part, write another Python code that attempts to open and read the
contents of "[Link]", displaying its full content on the screen; if the file is
missing, the program should handle the error gracefully by printing an appropriate
message such as "File not found. Please check the name.". Ensure both programs
use proper file handling (including the with statement) and exception handling
techniques.
Q7. Write a Python program that performs direct binary read and write operations using L2, L3
the struct module. The program should save a list of integers into a binary file and
then read them back to confirm correctness.
Input:
[10, 20, 30, 40, 50]
Output:
Integers written to binary file successfully.
Integers read from file: [10, 20, 30, 40, 50].
Q8. Write a Python program to compare storing fixed-length vs. variable-length strings L2, L3
in a file.
• Save a string in fixed length (20 characters padded with spaces).
• Save another string in variable length.
Read both back and explain the storage difference.
Input:
String: "Python"
Output:
Fixed length storage: 'Python ' (20 bytes)
Variable length storage: 'Python' (6 bytes).
Q9. Write a program to demonstrate the difference between Big Endian and Little L2, L3
Endian byte ordering using the struct module. Store the same integer in both formats
and display the raw byte sequences.
Input:
1025
Output:
Little Endian: b'\x01\x04'
Big Endian: b'\x04\x01'.
Q10. Write a Python program that uses the Pickling module. L2, L3
• Pickle a dictionary containing student records (name, age, grade) into a
binary file.
• Unpickle the data and display it.
Input:
{"Alice": (20, "A"), "Bob": (19, "B"), "Charlie": (21, "A")}
Output:
Pickled data saved successfully.
Unpickled data: {'Alice': (20, 'A'), 'Bob': (19, 'B'), 'Charlie': (21, 'A')}
-END-