Python Display file content with line number

 # File handling 

# Line number 


fs = None


try:

fs = open("Src.txt","r")

except IOError as e:

print("File opening error : ", e)

except:

print("unknown error")

else:

linenum=1

for line in fs:

print(linenum,line,end="")

linenum+=1

finally:

if(fs):

fs.close()


"""

Sample output

Input file: Src.txt

Hello!!!

Welcome!!!

IT

CSE

EEE

ECE

Welcome!!!

Python



>python FileLineNum.py

1 Hello!!!

2 Welcome!!!

3 IT

4 CSE

5 EEE

6 ECE

7 Welcome!!!

8 Python

"""


No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu