Which Python module is commonly used to delete files?
A
file B
os C
sys D
shutil
Analysis & Theory
The `os` module provides functions like `os.remove()` to delete files.
Which function is used to delete a file in Python?
A
os.delete() B
os.remove() C
os.destroy() D
os.clear()
Analysis & Theory
The correct function to delete a file is `os.remove()`.
What happens if you try to delete a file that does not exist using os.remove()?
A
The file is created B
It does nothing C
A FileNotFoundError is raised D
The file is hidden
Analysis & Theory
os.remove() raises a FileNotFoundError if the file does not exist.
How can you check if a file exists before deleting it?
A
os.is_file(file) B
file.exists() C
os.path.exists(file) D
file.check()
Analysis & Theory
Use `os.path.exists(filename)` to check if the file exists before deleting it.
Which of the following can delete a non-empty folder?
A
os.rmdir() B
os.remove() C
shutil.rmtree() D
os.del()
Analysis & Theory
Use `shutil.rmtree()` to delete a non-empty directory.
Which function is used to delete an empty folder?
A
os.delete() B
os.rmdir() C
os.remove() D
os.cleardir()
Analysis & Theory
`os.rmdir()` is used to remove an empty folder.
What should you import before using os.remove()?
A
import file B
import delete C
import os D
import shutil
Analysis & Theory
You must import the `os` module to use `os.remove()`.
Which function is best for deleting folders with files inside?
A
os.remove() B
os.rmdir() C
shutil.rmtree() D
os.deleteall()
Analysis & Theory
`shutil.rmtree()` is used to delete an entire folder and its contents.
What is the result of os.remove('myfile.txt') if the file is open?
A
Deletes file with warning B
Raises an error C
Closes and deletes the file D
Deletes after program exits
Analysis & Theory
Attempting to delete an open file may raise a permission error depending on the OS.
Which of the following is a safe way to delete a file?
A
os.remove(file) B
if os.path.exists(file): os.remove(file) C
os.rmdir(file) D
shutil.remove(file)
Analysis & Theory
Checking if the file exists before calling os.remove() prevents errors.