Showing posts with label display nth line of a file. Show all posts
Showing posts with label display nth line of a file. Show all posts

File Handling 5: Display nth line of a file

# Display nth line of a file
# Use command line arguments

import sys

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

Nline = int(input("Enter line to print : "))

L = fs.readlines()

print("File:", fs.name)
print("Line number -", Nline)
print(L[Nline-1])

fs.close()