File management is a frequently discussed topic in Python interviews, especially for roles that require working with data or implementing system utilities. Below are some of the most popular interview questions and answers related to file management in Python.
1. How do you open a file in Python?
Answer:
To open a file in Python, you can use the built-in open
function:
with open("example.txt", "r") as file:
content = file.read()
The with
statement ensures that the file is properly closed after its suite finishes.
2. What are the different file modes in Python?
Answer:
Python offers several modes for file operations:
r
: Read (default). Opens the file for reading.w
: Write. Opens the file for writing (creates a new file or truncates an existing file).x
: Exclusive creation. Creates a new file and opens it for writing.a
: Append. Opens the file for writing (creates the file if it does not exist).b
: Binary mode. Reads or writes the file in binary mode (e.g.,rb
orwb
).t
: Text mode (default).+
: Updating. Reading and writing (e.g.,r+
orw+
).
3. How do you read a file line-by-line?
Answer:
You can read a file line-by-line using a for loop:
with open("example.txt", "r") as file:
for line in file:
print(line.strip())
4. How do you write data to a file?
Answer:
You can write data to a file using the write
method:
with open("output.txt", "w") as file:
file.write("Hello, World!")
5. How do you copy a file in Python?
Answer:
You can copy a file using the shutil
library:
import shutil
shutil.copy("source.txt", "destination.txt")
6. What is the os
module and how is it used for file management?
Answer:
The os
module provides a way of interacting with the operating system. For file management, you can use it to list files in a directory, delete files, rename files, and more.
import os
os.rename("old_file.txt", "new_file.txt")
os.remove("unwanted_file.txt")
7. How can you check if a file or directory exists?
Answer:
You can use os.path.exists
to check if a file or directory exists:
import os
if os.path.exists("example.txt"):
print("The file exists.")
8. How do you list all files in a directory?
Answer:
You can list all files in a directory using os.listdir
:
import os
files = os.listdir(".")
print(files)
9. How can you read and write CSV files?
Answer:
You can read and write CSV files using Python’s csv
module:
import csv
# Reading
with open("data.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
# Writing
with open("data.csv", "w") as file:
writer = csv.writer(file)
writer.writerow(["Name", "Age"])
writer.writerow(["Alice", 30])
10. How can you work with JSON files in Python?
Answer:
You can work with JSON files using the json
module:
import json
# Writing
with open("data.json", "w") as file:
json.dump({"name": "Alice", "age": 30}, file)
# Reading
with open("data.json", "r") as file:
data = json.load(file)
print(data)
These questions should give you a strong foundation in file management in Python and prepare you well for related interview topics. Good luck!