Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Python example to read a file with exception handling

import sys

def getfilen():
    global fname
    fname = sys.ergv[1]
def getfileninp():
    global fname
    fname = raw_input("File Name: ")
try:
    getfilen()
except:
    getfileninp()
try:
    f = open(fname, "r")
except:
    print("\nBad Filename / Binary File")
    sys.exit()
linen = 1
for line in f:
    print("1: %s %s %s" % linen, fname, line)
    linen = linen + 1
print("EOF")

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.