Question

In: Computer Science

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

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.
    • 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.

Solutions

Expert Solution

CODE: Python Programming Language

# numberlines.py

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

============================================================================

SCREENSHOT OF THE CODE:

============================================================================

INPUT:

numberlines.txt

This is the first line of text.

OUTPUT:

numberlines_output.txt

Thank you.


Related Solutions

Write a script that creates and calls a stored procedure named test. This procedure should identify...
Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this: 2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3 1 2...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this: 2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3...
Write a program in a script file that creates m × n matrix with elements that...
Write a program in a script file that creates m × n matrix with elements that have the following values. The value of each element in the last row is the number of the column. The value of each element in the last column is the number of row +1. For the rest of the elements, each has a value equal to the sum of the element below it and the element to the right.
SQL Homework -- This script creates the schema named mgs -- Connect as the user named...
SQL Homework -- This script creates the schema named mgs -- Connect as the user named mgs --CONNECT cs270226mgs/mgs; -- Use an anonymous PL/SQL script to -- drop all tables and sequences in the current schema and -- suppress any error messages that may displayed -- if these objects don't exist BEGIN EXECUTE IMMEDIATE 'DROP SEQUENCE category_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE product_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE customer_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE address_id_seq'; EXECUTE IMMEDIATE 'DROP SEQUENCE order_id_seq'; EXECUTE IMMEDIATE 'DROP...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all...
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all its contents. Write a shell script to validate password strength. Here are a few assumptions for the password string.   Length – a minimum of 8 characters. • Contain alphabets , numbers , and @ # $ % & * symbols. • Include both the small and capital case letters. give a prompt of Y or N to try another password and displays an error...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Specification This script reads in class data from a file and creates a dictionary. It then...
Specification This script reads in class data from a file and creates a dictionary. It then prints the data from the dictionary. The script has the following functions class_tuple_create print_classes class_tuple_create This function has the following header def class_tuple_create(filename): The function reads in a file with four fields of data about a class. The fields are Course ID Room number Instructor Start time The function must print an error message if the file cannot be opened for any reason. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT