Delete File in Python on Mac - VicPhanDevOps/mac GitHub Wiki

• Press the command key and spacebar to launch Spotlight Search, type “terminal” and select the “Terminal” application.
image

• Type python3 and press the return key to launch the Python interactive shell.
image

• Type import os and press the return key to import the os module.
image

• Type from pathlib import Path and press the return key to import the Path object from the pathlib module.
image

• 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.
image

• Type os.path.exists(< variable >) and press the return key to test the file path and it will return True since the file exists.
image

• Type os.remove(< variable >) and press the return key to delete the file.
image

• 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.
image

• Type exit() or quit() and press the return key to quit the Python interactive shell and return the UNIX-like shell.
image

⚠️ **GitHub.com Fallback** ⚠️