Ask Question
18 August, 02:25

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

+4
Answers (1)
  1. 18 August, 02:32
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers