1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Otrada [13]
4 years ago
15

6.13: Word Count

Computers and Technology
1 answer:
lions [1.4K]4 years ago
3 0

Answer:

import os

def search_file(file_name):

       file_name += '.txt' # The code automatically adds the .txt extension, so the user is expected to just pass in the name of the file without extension

       for _, __, files in os.walk('.'):

               for file in files:

                       if file_name == file:

                               return file

       raise FileNotFoundError('File not found')

file_name = input('Enter the name of the file without extension > ')

file = search_file(file_name)

with open(file) as open_file:

       a = open_file.read()

       a = a.split()

       print('The file {}.txt contain {} words'.format(file_name, len(a)))

Explanation:

The above code is written in Python Programming Language. The first line of code is an import statement that imports the os module which is responsible for operating system's functionality in Python. This module is needed because we will be doing things like file search in the code.

The next thing that was done is that a function called search_file which accepts just a single argument was defined. The purpose of this function is to use the os module imported earlier and search for the existence of the file passed in as the argument. If the file is found, the file is returned else a FileNotFoundError is raised

Immediately after the function, the program expects an input from the user which should be the name of the file without the extension. It is also assumed that the filename passed in is a text file with the .txt extension.

After the program gets the file name from the user via the input function, the function we defined earlier is now called passing the value of the input as the argument to the function.

if file passed in does not exist, the program crashes there with the FileNotFound exception - thanks to the raise FileNotFoundError statement in the function.

However if the file exists, the file is opened and read. The contents of the file is splitted by whitespace. Then we use the Python's len function is use to count the items in the list after the split function is ran.

Finally, we print this to the console using string formatting

You might be interested in
Clicking a _____ name opens a drop-down list of commands and options. Select one: a. menu b. status bar c. ribbon d. toolbar
Elan Coil [88]

Answer:

menu

Explanation:

https://quizlet.com/231958668/windows-chapter-1-concepts-exam-flash-cards/

7 0
3 years ago
Which of the following BEST represents the relationship between physical fitness and exercise?
adoni [48]
Exercise is a subset of physical fittness
8 0
3 years ago
Which letter is located at position (7,4) on this coordinate grid? A) B) C) D)
aleksklad [387]

It is letter d

gdstcccccccvdagjsbhbj cna cas

8 0
3 years ago
The technique of ________ uses three columns that allows the entrepreneur to weigh both the advantages and the disadvantages of
Mekhanik [1.2K]
Force-field analysis
5 0
3 years ago
Which of the following menu commands would you select to make a copy of an open file and rename it
egoroff_w [7]
The menu command that you would <span>select to make a copy of an open file and rename it is SaveAs. </span>
5 0
3 years ago
Read 2 more answers
Other questions:
  • The leader of the team wants to do everything him or herself. They have a very big ego and are difficult to work with. What is t
    14·1 answer
  • Which of the following is not an example of a technological aid?
    11·1 answer
  • Individuals and businesses have concerns about data security while using Internet-based applications. Which security risk refers
    7·2 answers
  • WHICH COMPUTER COMPONENT CONTAINS ALL THE CIRCUITRY NECESSARY FOR THE OTHER COMPONENTS OR DEVICES TO COMMUNICATE WITH ONE ANOTHE
    12·1 answer
  • Given an 10-bit two's complement binary number, what is the decimal value of the largest negative integer that can be represente
    10·1 answer
  • The attacker sent data that was able to manipulate the return address that is reserved to store expected data
    5·1 answer
  • The Excel application is used to create
    7·1 answer
  • First computer to use Windows os?​
    5·1 answer
  • ______________ memory is a small, high-speed, high-cost memory that servers as a buffer for frequently accessed data
    9·2 answers
  • What is the decimal number 86 when written as a
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!