Ask Question

Assume that a file containing a series of cities (as strings) is named cities. txt and exists on the computer's disk. Write a program that displays the number of names that are stored in the file. (Hint: Open the file and read every string stored in it. Use a variable to keep a count of the number of items that are read from the file.)

+5
Answers (1)
  1. 20 March, 09:24
    0
    name = 'cities. txt'

    obj = open (name, 'r')

    ctr = 0

    for l in obj:

    ctr + = 1

    print ('Total ', ctr, ' names in this file.')

    obj. close ()

    Explanation:

    The file should be opened in read mode. Loop through to read each line and increase the counter. Display the cities in the file.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Assume that a file containing a series of cities (as strings) is named cities. txt and exists on the computer's disk. Write a program that ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers