File Handling 4: Display first n lines of a file

# Display first n lines of a file
# Use command line arguments

import sys

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

L = fs.readlines()

Nlines = int(input("Enter number of lines to print : "))

print("First", Nlines, "of File:", fs.name)
for Line in L[0:Nlines]:
print(Line)

fs.close()

No comments:

Post a Comment

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

Anu