Read File in Python - 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 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.
•
• 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.
•
• 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.
•
• Type print(< read variable >.read())
and press the return key to print the contents of the file in the shell.
•
• Type quit()
or exit()
and press the return key to exit the Python shell and return to the UNIX-like shell.
•