Second File Handling Test 1
Second File Handling Test 1
1
Write a Python program to open a
text file named "data.txt" in read
mode, use seek() to navigate to the
20th byte, read the next 100
characters, and display the content.
'''
f = open('data.txt', mode='r')
f.seek(20)
content = f.read(100)
print(content)
f.close()
''' Q.2
Differentiate between the return
type and usage of the methods
read(), readline() and readlines()
in Python.
'''
read():
Return Type: str
Usage: The read() method is used to read the entire contents of a file as a single string.
It reads from the current file position to the end of the file or up to the specified number of bytes,
if provided as an argument. If you don't specify the number of bytes to read, it will read the entire file.
Example:
with open('file.txt', 'r') as file:
data = file.read()
readline():
Return Type: str
Usage: The readline() method is used to read the next line from the file, starting from the current file position
It returns the content of the line as a string, including the newline character at the end.
If you call readline() multiple times, it will read subsequent lines one by one.
Example:
with open('file.txt', 'r') as file:
line1 = file.readline()
line2 = file.readline()
readlines():
Return Type: list of str
Usage: The readlines() method is used to read all the lines from the current file position to the end of the
and return them as a list of strings. Each string in the list represents a line from the file.
You can iterate through this list to access each line of the file.
Example:
with open('file.txt', 'r') as file:
lines = file.readlines()
for line in lines:
# Process each line
In summary, the key differences between these methods are in their return types and
how they read data from a file. read() returns the entire file content as a single string,
readline() reads one line at a time, and readlines() reads all lines and returns them as a list of strings.
The choice of method depends on your specific needs when working with files in Python.
In this example, we convert the integer 42 into a 4-byte byte stream in big-endian byte order.
In this example, we convert the 4-byte byte stream b'\x00\x00\x00\x2A' back to the integer value 42.
You can adjust the number of bytes and the byte order according to your specific requirements.
'''Q.3
Write a Python program to read
integers from a binary file
"numbers.bin" and print them in
reverse order using seek() and
tell() to demonstrate the file
pointer's movement.
'''
f = open('numbers.bin', 'rb')
f.seek(-1,2)
file_pointer_position=f.tell()
i = 2
while file_pointer_position >= 0:
print(int.from_bytes(f.read(1)), end=" ")
if file_pointer_position == 0:
break
f.seek(-i,2)
file_pointer_position = f.tell()
i+=1
f.close()
''' Q.4
Write a Python program to create a
list of dictionaries, each
containing information about a
product, and save it as a binary
file named "products.pkl" using the
pickle module.
'''
import pickle
''' Q.5
Write a Python program to load and
display the content of the
"products.pkl" file created in
question 4. Perform data
manipulation on the loaded data,
such as sorting products based on
their prices in ascending order.
'''
import pickle
# Load the list of products from the binary file using pickle
with open(file_name, 'rb') as file:
products = pickle.load(file)
''' Q.6
Write a Python program to analyze a
text file named "info.txt" and
display the following information:
-Total number of characters
-Total number of words
-Total number of lines
'''
file_name = "data.txt"
''' Q.6
Write a function count_Dwords() in
Python to count the words
ending with a digit in a text file
"Details.txt".
Example:
If the file content is as follows:
On seat2 VIP1 will sit and
On seat1 VVIP2 will be sitting
Output will be:
Number of words ending with a digit
are: 4
'''
f = open('Details.txt', 'r')
Digit_ended_word_count = 0
text_file_content = f.read().split()
print(Digit_ended_word_count)
'''Q.8
Explain the purpose and
functionality of the Python pickle
module in binary file handling,
including its support for complex
data structures.
'''
'''
The Python `pickle` module is used for binary file handling and provides a way to serialize
and deserialize Python objects. It allows you to save Python objects to binary files and
load them back into memory, preserving their data structures. T
3. Cross-Version Compatibility:
- Pickle files can be used to transfer data between different versions of Python,
as the module handles the details of data representation and Python version compatibility.
Here's a basic example of using the `pickle` module to serialize and deserialize a Python object:
'''
import pickle
# Data to be serialized
data = {'name': 'Alice', 'age': 30, 'city': 'Wonderland'}
'''
In this example, we use `pickle.dump()` to save the `data` dictionary to a binary file,
and then we use `pickle.load()` to load it back into memory.
The loaded data is an exact copy of the original dictionary,
preserving its data structure and contents.