try: myFile=open("/home/aditya1117/filename.txt",mode="r") for line in myFile: print(line) except Exception as e: print(str(e)) finally: myFile.close() Output: This is a sample text file. This file is for PythonForBeginners.com. This file has been written by Aditya Raj for de...
To open a file in python using the os module, we can use the open() method. The open() method takes two arguments as input. The first argument is the filename which needs to be opened and the second argument is the mode in which the file has to be opened. The most common mode ...