Showing posts with label Display first n lines of a file. Show all posts
Showing posts with label Display first n lines of a file. Show all posts

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()