File Handling 3: Display file content with line number

# Display file content with line number
# Use command line arguments

import sys

fname = sys.argv[1]

fs = open(fname, "r")
Line_num = 1

print("File:", fs.name)

while(True):
Line = fs.readline()

if not Line:
break

print(Line_num, Line)
Line_num += 1

fs.close()


# Display file content with line number
# Use command line arguments

import sys

fname = sys.argv[1]
fs = open(fname, "r")

print("File:", fs.name)

L = fs.readlines()
for Line in L:
print(L.index(Line)+1, Line)

fs.close()

No comments:

Post a Comment

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

Anu