Ask Question
16 March, 12:46

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

+3
Answers (1)
  1. 16 March, 15:11
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers