Question

Finish this Code! We want to open a file called "names.txt" and display all of the...

Finish this Code!

We want to open a file called "names.txt" and display all of the lines of the file as a list.

  1. Open the file in "reading" mode
  2. Use readlines to store the file into a list
  3. Use list indexing to display the 3rd item in the list
  4. This step is done for you! Close the file

When you are finished, the output should match Desired Output.

Notice: due to limitations with readine files in an online environment, the line breaks will display as \\\\n. Do not be alarmed!

# Open the File
fin =

# Use readlines to store
# the file in a list


# Use indexing to
# display the 3rd line

# Close the file
fin.close()

Desired output is

Ron Weasley

Homework Answers

Answer #1

Source Code:

Output:

Code in text format (See above image of code for indentation):

# Open the File
fin =open("names.txt",'r')
# Use readlines to store
# the file in a list
file=[name for name in fin.readlines()]
# Use indexing to
# display the 3rd line
print(file[2])
# Close the file
fin.close()

names.txt file: (This file and source code file must be in same location)

John Warn
Smith Soley
Ron Weasly
Tim Lee
Bruce Lee
Berners Lee
Canady Joseph
Joseph Canady

Know the answer?
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
Similar Questions
Code a C file, following these instructions: This lab is about getting the input from a...
Code a C file, following these instructions: This lab is about getting the input from a file. Let’s assume the words are provided in one file, passed as an argument to your program. The names of the files are provided as arguments to your program (i.e., they are copied by the shell in the argv variable), and each file is a text file with lots of words. Open the manual page for open() system call and add to your code...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If you can't implement all of the features of Project described in this document, implement what you can. Start by implementing the StockItem class, and its derived classes. Then add in the InventoryManager class later. In each case, the test files must be in separate classes. UML Diagram: Use Violet or other drawing software to draw the UML diagrams of each class that you use...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...