Question

In: Computer Science

In Python please:  Number the Lines in a File: Create a program that adds line numbers to...

In Python please:  Number the Lines in a File: Create a program that adds line numbers to a file. The name of the input file will be read from the user, as will the name of the new file that your program will create. Each line in the output file should begin with the line number, followed by a colon and a space, followed by the line from the input file.

Solutions

Expert Solution

Program code to copy:-

#This program adds line numbers to a input file and
#stores the content of file with line number into another file.
#Name of both input and output file entered by the user.

#Prompt the user to enter input file name
inputFileName = input('Enter the input file name: ')
#Prompt the user to enter output file name
outputFileName = input('Enter the input file name: ')

#Open the file for reading
fin = open(inputFileName, "r")
#Open the file for writing
fout = open(outputFileName, "w")


lineNo = 1 #Used to keep track of line number

#Loop read the content of input file line by line and
#store each line into variable 'line' till end of input file
for line in fin:
#Writing line with line number into output file
fout.write(str(lineNo) + ": " + line)
lineNo = lineNo + 1

#Closing both input and output file
fin.close()
fout.close()

Screenshot of Program Code:-

Screenshot of program output:-

Screenshot of input file:-

Screenshot of output file:-

Note:- Before executing above program code you need to create a input file and store it into current working folder.


Related Solutions

Using the Python program: a/ Write a program that adds all numbers from 1 to 100...
Using the Python program: a/ Write a program that adds all numbers from 1 to 100 to a list (in order). Then remove the multiples of 3 from the list. Print the remaining values. b/ Write a program that initializes a list with ten random integers from 1 to 100. Then remove the multiples of 3 and 5 from the list. Print the remaining values. Please show your work and explain. Thanks
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers from the user until they enter a 0 and computes the product of all these numbers and outputs it. Hint: use the example from the slides where we compute the sum of a list of numbers, but initialize the variable holding the product to 1 instead of 0. print("Enter n") n = int(input()) min = n while n != 0: if n < min:...
In Python: read in 'marks.txt' . The file format is one number per line. Your program...
In Python: read in 'marks.txt' . The file format is one number per line. Your program will run through all these numbers, and make sure that they are valid input. you have to Create two lists: one for valid numbers and one for invalid numbers. For a number to be valid the number has to: Must be less than or equal to 100 Must be greater than or equal to 0 Must not have any letters inside of it (it...
Solve please in python b) Create a program that shows a series of numbers that start...
Solve please in python b) Create a program that shows a series of numbers that start at a and increase from 5 to 5 until reaching b, where a and b are two numbers captured by the user and assumes that a is always less than b. Note that a and b are not necessarily multiples of 5, and that you must display all numbers that are less than or equal to b. c) Create a program that displays n...
create a file with 1000 alphanumeric ones written one per line. Write a program in python...
create a file with 1000 alphanumeric ones written one per line. Write a program in python that randomly selects 100 of them, holds the index of each alphanumeric, adds the hash of the previous 100 and tries to find a random alphanumeric so that if it adds it to the list of indicators the SHA256 of the whole to have 10 zeros at the end. You start with the original Hash: 1111..111 my solution so far: import random import string...
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
IN PYTHON: Write a program that displays the lines from the total.txt file in the following...
IN PYTHON: Write a program that displays the lines from the total.txt file in the following output. Use a try catch phrase to check for errors. Use only one function for this portion [main()]. Remember to use Python. Sample output below: 19 16, 29 3, 30 4, 34
NASM - Create a program that adds two binary numbers and returns the result in binary.
NASM - Create a program that adds two binary numbers and returns the result in binary.
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
1)  Write a python program that opens a file, reads all of the lines into a list...
1)  Write a python program that opens a file, reads all of the lines into a list of strings, and closes the file. Use the Readlines() method. Test your programing using the names.txt file provided. 2) Convert the program into a function called loadFile, that receives the file name as a parameter and returns a list of strings. 3) Write a main routine that calls loadFIle three times to load the three data files given into three lists. Then choose a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT