95.2k views
4 votes
4. Write a script called lineCounter that accepts a filename as a command line argument. It should run through the file line by line, displaying a character count for each line. The script should include a line counter, which records the line number, and is incremented when each line is processed.

User Leeft
by
5.1k points

1 Answer

6 votes

Answer:

from sys import argv

script, filename = argv

with open( filename ) as file:

line_counter = 0

while True:

extract_line = file.readlines( )

line_counter += 1

character_count = extract_line.count( )

print( " line number: { } , character count: { }". format( line_counter, character_counter )

print ( " Nothing left to count" )

file.close( )

Step-by-step explanation:

The 'argv' is a module in the python system package, which allows for user input and script execution in the command. It loads the script and the file as a tuple which is offloaded in the script like arguments in a function.

User Brennan Mann
by
5.1k points