Delete File in Python on Mac - DevPops-Inc/python GitHub Wiki
• Press the command key and spacebar to launch Spotlight Search, type “terminal” and select the “Terminal” application.
•
• Type python3
and press the return key to launch the Python interactive shell.
•
• Type import os
and press the return key to import the os module.
•
• Type from pathlib import Path
and press the return key to import the Path
object from the pathlib
module.
•
• Type variable = Path(“< file path >”)
and press the return key to declare a variable and set it to the path of the file you wish to delete.
•
• Type os.path.exists(< variable >)
and press the return key to test the file path and it will return True
since the file exists.
•
• Type os.remove(< variable >)
and press the return key to delete the file.
•
• Press the up arrow twice to recall os.path.exists(< variable >)
and press the return key to sanity check that you’ve successfully deleted the file and it will return False
this time around.
•
• Type exit()
or quit()
and press the return key to quit the Python interactive shell and return the UNIX-like shell.
•