What is file handling in Python?
Answer
Python has built-in file I/O capabilities. Open a file: f = open("file.txt", "r") — modes: "r" (read), "w" (write, overwrites), "a" (append), "x" (create, fails if exists), "b" (binary), "+" (read+write). Always use the with statement to ensure files are closed: with open("file.txt", "r") as f: content = f.read(). Read methods: read() (entire file), readline() (one line), readlines() (list of lines). Iterate line by line (memory efficient): for line in f. Write: f.write("text"), f.writelines(list). For paths, use pathlib: from pathlib import Path; Path("file.txt").read_text(). Binary files: open with "rb" or "wb" mode. Check existence: Path("file").exists().
Previous
What are Python's comparison and logical operators?
Next
What is the difference between deep copy and shallow copy in Python?