Create a program that prints the lines of a file. This program must receive three parameters via the command line: the file’s name, the starting line, and the last line to print.
Answer:
##############################################################################
# Python From Scratch
# Author: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2025 - LogiKraft 2025
# Site: https://pythonfromscratch.com
# ISBN: 978-85-7522-949-1 (Paperback), 978-85-7522-950-7 (hardcover), 978-85-7522-951-4 (ebook)
#
# File: chapter 09/exercise-09-13.py.py
##############################################################################
# Identical to exercise 9.02
import sys
# Check if parameters were provided
if len(sys.argv) != 4: # Remember that the program name is the first in the list
print("\nUsage: exercise-09-13.py filename start end\n\n")
else:
name = sys.argv[1]
start = int(sys.argv[2])
end = int(sys.argv[3])
file = open(name, "r")
for line in file.readlines()[start - 1 : end]:
# Since the line ends with ENTER,
# we remove the last character before printing
print(line[:-1])
file.close()
# Don't forget to read about encodings
# Depending on the file type and your operating system,
# it may not print correctly on screen.