Question

Programming Exercise 4.9

Write a script named numberlines.py. This script creates a program listing from a source program.

This script should:

Prompt the user for the names of two files.

The input filename could be the name of the script itself, but be careful to use a different output filename!

The script copies the lines of text from the input file to the output file, numbering each line as it goes.

The line numbers should be right-justified in 4 columns, so that the format of a line in the output file looks like this example:

   1> This is the first line of text. 

Programming Exercise 4.9 Instructions Write a script named numberlines.py. This script creates a program listing from a source program This script should: 1. Prompt the user for the names of two files. o The input filename could be the name of the script itself, but be careful to use a different output filename! 2. The script copies the lines of text from the input file to the output file, numbering each line as it goes. 3. The line numbers should be right-justified in 4 columns, so that the format of a line in the output file looks like this example: 1> This is the first line of text.

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
input_filename = input('Enter input file name: ')
output_filename = input('Enter output file name: ')

with open(input_filename, 'r') as f, open(output_filename, 'w') as w:
    number = 0
    for line in f:
        number += 1
        w.write('{:>4}> {}'.format(number, line))
Add a comment
Know the answer?
Add Answer to:
Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT