CH 2 Exercise
CH 2 Exercise
1. Differentiate between:
a) text file and binary file
Text file Binary file
Stores data in readable characters (e.g., .txt) Stores data in binary format (e.g., .bin, .exe)
Can be opened and read easily in text Need specific software to read or write the
editors. contents of a binary file
EOL of character is newline (\n) No delimiter is required
Translator is required We do not require translator
readlines()
Reads all lines and returns them as a list
Syntax: file_object.readlines()
Eg: line=open(“file.txt”,’r’)
line = file.readlines()
line.close()
writelines()
Writes a list of strings to the file.
Eg: file.writelines(["Hello\n", "World\n"])
b) read()
Use: Reads the content of a file. If no argument or a negative number is specified in read(),
the entire file content is read.
Syntax: content = file.read()
c) seek()
Use: It is used to position the file object at a particular position in a file.
Syntax: file.seek(0) # Goes to the beginning of the file
d) dump()
Use: Writes Python objects to a binary file using the pickle module.
Syntax: import pickle
pickle.dump(obj, file)
3. Write the file mode that will be used for opening the following files. Also, write the
Python statements to open the following files:
a) A text file “example.txt” in both read and write mode
Mode: "r+"
Statement: f = open("example.txt", "r+")
4. Why is it advised to close a file after we are done with the read and write operations?
What will happen if we do not close it? Will some error message be flashed?
Ans:
Closing a file:
• System frees the memory allocated.
• Ensures that all data is properly flushed off( written) to the file before it is closed.
• Helps avoid file corruption or data loss.
If we don’t close it:
• Data might not be saved properly.
• No error may appear immediately, but it can cause bugs or memory leaks in larger
programs.
Python won’t always show an error if you forget to close a file. But unpredictable bugs might
occur like missing data, locked files, or slow performance.
5. What is the difference between the following set of statements (a) and (b):
a) P=open(“practice.txt”,”r”) P.read(10)
File is opened and stays open until manually closed. Reads first 10 characters.
6. Write a command(s) to write the following lines to the text file named hello.txt.
Assume that the file is opened in append mode.
“ Welcome my class”
“It is a fun place”
“You will learn and play”
f.write("Welcome my class\n")
f.write("It is a fun place\n")
f.write("You will learn and play\n")
7. Write a Python program to open the file hello.txt used in question no 6 in read mode
to display its contents. What will be the difference if the file was opened in write
mode instead of append mode?
Ans:
with open("hello.txt", "r") as f:
content = f.read()
print(content)
• Append mode ("a"): Adds new data to the end without deleting existing data.
• Write mode ("w"): Deletes existing data in the file before writing.
8. Program to accept sentences till "END", save to file, and display sentences starting
with uppercase letter:
9. Define pickling and explain serialization/deserialization:
• Pickling is the process of converting a Python object into a byte stream so that it can
be saved to a file or transferred over a network.
• Serialization: Serialization is the process of transforming data or an object in memory
(RAM) to a stream of bytes called byte streams. These byte streams in a binary file
can then be stored in a disk or in a database or sent through a network. (e.g., using
pickle.dump()).
• Deserialization: De-serialization or unpickling is the inverse of pickling process
where a byte stream is converted back to Python object.