Read File in Python - 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 file 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 read.
image

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

• Type < read variable >=open(“< file variable >”) and press the return key to declare a read variable and set it to the file you wish to read.
image

• Type print(< read variable >.read()) and press the return key to print the contents of the file in the shell.
image

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

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