1. What is file handling in Python?

 

File handling in Python refers to the process of working with files such as creating, opening, reading, writing, and closing files.
Python provides built-in functions to perform these operations easily.

2. Why is file handling important?

File handling is important because it allows programs to store data permanently. Without files, data would be lost when the program stops.

3. Which function is used to open a file in Python?

The open() function is used to open a file in Python.

file = open("example.txt", "r")

4. What are the different modes used in open() function?

  • r – Read mode
  • w – Write mode
  • a – Append mode
  • x – Create mode
  • b – Binary mode
  • t – Text mode

5. What is read mode?

Read mode (r) is used to read the contents of a file. The file must exist.

6. What is write mode?

Write mode (w) creates a new file or overwrites an existing file.

7. What is append mode?

Append mode (a) adds data at the end of the file without deleting existing data.

8. What is create mode?

Create mode (x) creates a new file and gives an error if file already exists.

9. What is binary mode?

Binary mode is used for handling binary files like images and videos.

10. What is text mode?

Text mode is used for handling text files such as .txt files.

11. How to read entire file in Python?

file = open("example.txt", "r")
content = file.read()
print(content)
file.close()

read() reads entire file content.

12. What is readline()?

readline() reads one line at a time from the file.

13. What is readlines()?

readlines() reads all lines and returns them as a list.

14. How to write into a file?

file = open("example.txt", "w")
file.write("Hello World")
file.close()

15. What does write() function do?

write() writes text into a file.

16. What is file closing?

Closing a file releases system resources.

17. Why is close() important?

close() prevents memory leaks and ensures data is saved properly.

18. What is with statement in file handling?

with open("example.txt", "r") as file:
print(file.read())

It automatically closes the file.

19. What is file object?

File object is returned by open() function and used to perform file operations.

20. What is file pointer?

File pointer indicates current position in file.

21. What is tell() function?

tell() returns current position of file pointer.

22. What is seek() function?

seek() changes file pointer position.

23. Example of seek()

file.seek(0)

24. What is difference between write and append?

  • write overwrites file
  • append adds data

25. What happens if file does not exist in read mode?

Python gives FileNotFoundError.

26. What is FileNotFoundError?

It occurs when file does not exist.

27. How to handle file exceptions?

try:
file = open("example.txt")
except FileNotFoundError:
print("File not found")

28. What is file path?

File path is location of file.

29. What is absolute path?

Full path from root directory.

30. What is relative path?

Path relative to current directory.

31. What is flush()?

flush() saves data immediately to disk.

32. What is writable()?

Checks if file is writable.

33. What is readable()?

Checks if file is readable.

34. What is closed attribute?

Checks if file is closed.

35. What is encoding?

Encoding defines character format like UTF-8.

36. What is file buffering?

File buffering is a process where data is temporarily stored in memory before writing to disk. This improves performance by reducing direct disk access.

37. What is default buffering mode in Python?

Python uses full buffering by default for files and line buffering for terminal output.

38. What is the difference between text file and binary file?

  • Text file stores readable characters (example: .txt)
  • Binary file stores data in binary format (example: .jpg, .mp3)

39. How to open file in binary mode?

file = open("image.jpg", "rb")

40. Why binary mode is used?

Binary mode is used to read or write non-text files like images, videos, and audio files.

41. What is file iteration?

File iteration means reading file line by line using loop.

with open("file.txt", "r") as file:
for line in file:
print(line)

42. What is difference between read() and readline()?

  • read() reads entire file
  • readline() reads one line

43. What is difference between readline() and readlines()?

  • readline() returns single line
  • readlines() returns list of lines

44. What is append mode advantage?

Append mode prevents overwriting and adds new data safely.

45. What happens when write mode is used on existing file?

It deletes old content and writes new content.

46. What is file pointer position after opening file?

File pointer starts at position 0.

47. What is seek(0)?

It moves file pointer to beginning.

48. What is seek(5)?

It moves pointer to position 5.

49. What does tell() return?

It returns current file pointer position.

50. What is file descriptor?

File descriptor is an internal number used by OS to identify file.

51. What is CSV file?

CSV stands for Comma Separated Values. It stores tabular data.

52. Which module is used for CSV file handling?

csv module is used.

53. How to read CSV file?

import csv
with open("file.csv") as file:
reader = csv.reader(file)

54. How to write CSV file?

import csv
with open("file.csv", "w") as file:
writer = csv.writer(file)

55. What is JSON file?

JSON is used for storing structured data.

56. Which module is used for JSON?

json module.

57. How to read JSON file?

import json
file = open("file.json")
data = json.load(file)

58. How to write JSON file?

json.dump(data, file)

59. What is pickle module?

Pickle module is used to store Python objects into file.

60. What is pickling?

Converting object into byte stream.

61. What is unpickling?

Restoring object from byte stream.

62. Why close file manually?

To free memory and avoid corruption.

63. What is advantage of with statement?

Automatically closes file.

64. What is file exception?

Error that occurs during file operations.

65. Example of exception handling?

try:
file=open("a.txt")
except:
print("Error")

66. What is OS module?

OS module helps manage files and directories.

67. How to delete file?

import os
os.remove("file.txt")

68. How to rename file?

os.rename("old.txt","new.txt")

69. How to check file exists?

os.path.exists("file.txt")

70. What is file size checking?

os.path.getsize("file.txt")

71. What is writable()?

Checks if file can be written.

72. What is readable()?

Checks if file readable.

73. What is file mode r+?

Read and write mode.

74. What is w+ mode?

Write and read mode.

75. What is a+ mode?

Append and read mode.

76. What is file encoding?

It defines character representation.

77. What is UTF-8?

Most common encoding format.

78. What is file corruption?

When file data becomes damaged.

79. What causes file corruption?

  • Improper closing
  • System crash
  • Virus

80. How to prevent corruption?

Always close file properly.

81. What is temporary file?

Temporary file is used for short-term storage.

82. Which module creates temp files?

tempfile module.

83. What is file permission?

Controls file access.

84. What are permission types?

  • Read
  • Write
  • Execute

85. What is file system?

System that manages files.

86. What is file handling advantage?

  • Permanent storage
  • Data reuse

87. What is disadvantage?

  • Slow compared to memory

88. What is best practice?

Use with statement.

89. What is file security?

Protecting file from unauthorized access.

90. What is log file?

File storing program logs.

91. What is data persistence?

Saving data permanently.

92. What is file management?

Organizing files.

93. What is file storage?

Saving data in file.

94. What is file retrieval?

Reading data from file.

95. What is structured file?

Organized format like CSV.

96. What is unstructured file?

Unorganized data.

97. What is file access time?

Time taken to access file.

98. What is file write time?

Time taken to write file.

99. What is file read time?

Time taken to read file.

100. Why file handling is important in Python?

File handling allows storing, retrieving, and managing data permanently, which is essential for real-world applications.

📢 Join Our WhatsApp Channel

💼 Get Daily IT Job Updates, Interview Preparation Tips & Instant Alerts directly on WhatsApp.

👉 Join WhatsApp Now

📢 Join Our Telegram Channel

💼 Get Daily IT Job Updates, Interview Tips & Exclusive Alerts directly on Telegram!

👉 Join Telegram

Leave a Reply

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

Copyright © 2022 - 2025 itfreesource.com

Enable Notifications OK No thanks