FILE HANDLING VIVA

1. What is a data file in Python, and why is it used?
Sol:- A data file stores information for long-term use. Python supports text and binary files for storing and retrieving data.
2. What are the two main types of data files?
Sol:- Text files and binary files.
3. Explain the difference between text files and binary files.
Sol:-Text files store data as readable text. Binary files store data in a raw binary format.
4. How do you open a file in Python? What is the syntax of the open() function?
Sol:- file = open(“filename.txt”, “mode”)
5.What are different file modes in Python?
Sol:- ‘r’, ‘w’, ‘a’, ‘x’, ‘r+’, ‘w+’, ‘a+’.
6.Explain the purpose of ‘r’, ‘w’, ‘a’, and ‘x’ modes in file handling.
Sol:-
‘r’: Read-only mode.
‘w’: Write-only mode (overwrites existing content).
‘a’: Append mode.
‘x’: Create mode (raises an error if the file exists).
7. What is the difference between ‘w’ and ‘a’ mode?
Sol:- ‘w’ overwrites the file, while ‘a’ appends to it.
8. What is the role of the ‘b’ mode in file handling?
Sol:- It is used to handle binary files.
9. What happens if you try to open a non-existent file in ‘r’ mode?
Sol:- Raises a FileNotFoundError.
10.Explain the use of the with statement in file handling.
Sol:-  Ensures the file is properly closed after its block is executed.
with open(“file.txt”, “r”) as file:
data = file.read()
11. How is data stored in a text file?
Sol:- Data is stored as plain text, line by line.
12. What are the differences between read(), readline(), and readlines() functions?
Sol:-
read(): Reads the entire file.
readline(): Reads one line at a time.
readlines(): Reads all lines and returns them as a list.
13. How do you write data to a text file in Python?
Sol:- Use write() or writelines()
with open(“file.txt”, “w”) as file:
file.write(“Hello, World!”)
14. What is the difference between overwriting and appending to a text file?
Sol:- Overwriting (‘w’) clears the file’s content, while appending (‘a’) retains existing content and adds new data.
15. What does the tell() method do in file handling?
Sol:- Returns the current position of the file pointer.
16. Explain the purpose of the seek() method.
Sol:- Moves the file pointer to a specific position.
file.seek(0) # Moves to the beginning
17. How is data stored in a binary file?
Sol:- As binary data (0s and 1s).
18. What is the difference between text and binary files in terms of data storage?
Sol:- Text files store data as characters; binary files store raw bytes.
19. How do you read and write binary files in Python?
Sol:- Use ‘rb’ and ‘wb’ modes.
with open(“file.bin”, “wb”) as file:
file.write(b”Hello”)
20. Explain the use of the pickle module in working with binary files.
Sol:- It serializes and deserializes Python objects into binary format.
21. How do you serialize and deserialize data using pickle?
Sol :-
import pickle
data = {“name”: “Alice”, “age”: 25}
with open(“data.pkl”, “wb”) as file:
pickle.dump(data, file)
with open(“data.pkl”, “rb”) as file:
result = pickle.load(file)
print(result)
22. What is the purpose of dump() and load() methods in the pickle module?
Sol:- dump() serializes data, and load() deserializes data.
23. How do you handle binary data in Python without the pickle module?
Sol:- Use struct or array modules.
24. Write a Python statement to write a list of numbers to a binary file.
Sol :-
with open(“numbers.bin”, “wb”) as file:
file.write(bytearray([1, 2, 3]))
25. What happens if you try to read binary data using read() from a text file?
Sol:- The data is returned as a string representation of bytes.
26. How do you convert binary data into readable format while debugging?
Sol:- Use int.from_bytes() or decode the binary data.
27.What is a CSV file?
Sol:- A file where data is stored in a tabular format, separated by commas.
28. How is data structured in a CSV file?
Sol:- Data is stored as rows and columns, separated by delimiters.
29. What module is used in Python to handle CSV files?
Sol:- csv.
30. Explain the difference between csv.reader and csv.writer.
Sol:- csv.reader reads data, and csv.writer writes data to a CSV file.
31. What is the purpose of the DictReader and DictWriter classes in the csv module?
Sol:- To work with rows as dictionaries.
32.How do you specify a custom delimiter in a CSV file?
Sol:- Use the delimiter parameter in csv.reader or csv.writer.
33.What happens if a CSV file contains inconsistent rows while reading it?
Sol:- It can raise an error or handle it based on the logic.

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.