Question

.Write a program to prompt the user for a file name and a linenumber...

Write a program to prompt the user for a file name and a line number and then print only that line from the file. Consider possible difficulties and include code to address them. [Hints: it is important to clearly define what you think “line” means. Also, there are a number of errors to consider trapping.]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANS)

For example let us consider my program that allows the user to navigate through the lines of text in any text file. The program prompts the user for a filename and copies the lines of text from the file into a list. The program then prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program quits. Otherwise, the program prints the text in that line number.


code
enterfile = input("Enter the file name: ")
file = open(enterfile, 'r')
linecount = 0

for line in file:
linecount = linecount + 1

print("The number of lines in this txt. file is", linecount)

while True:
linenum = 0

num = int(input("Please enter a line number or press 0 to quit: "))
if num >=1 and num <= linecount:
file = open(enterfile, 'r')
for lines in file:
linenum = linenum + 1
if linenum == num:
print(lines)
else:
if num == 0:
print("Thanks for using the program")
break
Move line linenum = 0 inside the While True: loop.

The linenum variable must be reset to 0 (linenum = 0) when the program re-enters the loop. Otherwise the linenum variable will always keep being incremented and have a value that is greater than num and will never trigger the if statement to print the line at that number.

Alternative method:

enterfile = input("Enter the file name: ")

with open(enterfile) as f:
lines = [line.rstrip() for line in f]

print("The number of lines in this txt. file is", len(lines))

while True:
num = int(input("Please enter a line number or press 0 to quit: "))

if num > 0 and num < len(lines) + 1:
print(lines[num - 1])
elif num == 0:
print('Thanks for using the program.')

Add a comment
Know the answer?
Add Answer to:
.Write a program to prompt the user for a file name and a linenumber...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Write a PYTHON program that reads a file (prompt user for the input file name) containing...

    Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1   0.5 2   0.5 3   0.5 4   0.5 The output should be:    The averages are 2.50 and 0.5. a)   Your code with comments b)   A screenshot of the execution Version 3.7.2

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • Python Error: Writing a program to complete the following: Prompt the user for the name of...

    Python Error: Writing a program to complete the following: Prompt the user for the name of an input file. Open that file for input. (reading) Read the file using a "for line in a file" loop For each line,  o print the line and compute and print the average word length for that line. Close the input file Sample output: MY CODE IS WRONG AND NEED HELP CORRECTING IT!!! ------------------------------------------------------------------------------------------------------------------------ DESIRED OUTPUT: Enter input file name: Seasons.txt Thirty days hath September...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Write a x64 MASM program using ReadConsoleA and WriteConsoleA to prompt the user for their name....

    Write a x64 MASM program using ReadConsoleA and WriteConsoleA to prompt the user for their name. The program should then display the user's name surrounded by asterisks. Allow the name to be up to 30 characters long. Example (user input is boldface, and there are 28 asterisks on the top and bottom lines): What is your name? Kristin Jacobs **************************** *Kristin Jacobs * **************************** Copy / Paste a sample run from the console screen into your .asm file following your...

  • Write a "PYTHON" program to prompt the user to enter a fist name, last name, student...

    Write a "PYTHON" program to prompt the user to enter a fist name, last name, student ID and GPA. Create a dictionary with the data. Print out the data. Then remove the GPA and print again.

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains...

    Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains a Java program. Your program should read the file (e.g., InputFile.java) and output its contents properly indented to ProgramName_Formatted.java (e.g., InputFile_Formatted.java). (Note: It will be up to the user to change the name appropriately for compilation later.) When you see a left-brace character ({) in the file, increase your indentation level by NUM_SPACES spaces. When you see a right-brace character (}), decrease your indentation...

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