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]
3 years ago
15

6.13: Word Count

Computers and Technology
1 answer:
lions [1.4K]3 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
Alice is working on a web page to attract the maximum number of people to join her cause. She wants to inform people about the b
Deffense [45]

Answer:

Option D.    Persuasive writing style

is correct answer.

Explanation:

  • Alice should adopt the persuasive writing style as she wants to convince the people to adopt eco-friendly methods of living.
  • When a person write persuasively, this means he/she will present justifications and reasons for that specific opinion he/she is trying to convince the audience.
  • For the correctness of their opinion, different kinds of evidences are prepared so that the fact persuade the reader.
  • Persuasive writing is used widely while writing academic papers, advertisements  and argumentative essays as well.
<h2></h2><h2>i hope it will help you!</h2>

7 0
4 years ago
Giving 5 stars, a Thanks, 80 points, and Branliest to whoever answers them correctly.
Mrac [35]

Answer:

Explanation:

The answer is ludonarrative.

Please mark me brainliest.

4 0
3 years ago
PLEASE HELP ITS TIMED Ishmael would like to capture a selected portion of his screen and then capture actions he performs on tha
jek_recluse [69]

Answer:

Option B

<u><em>Hope this Helps!!! :)</em></u>

6 0
3 years ago
I want to discard my old computer and securely erase the data from my hard drive.
FrozenT [24]
Wipe off the hard drive with a software eraser but it won't be quick and won't work on defective disks
3 0
3 years ago
What is a form of technology that you think will make your life easier?
zzz [600]

Answer:

Digital communication technologies

Explanation:

connecting people across the globe has become easier and faster. Platforms such as Zoom, Room, Microsoft teams, WebEx, and many others are buzzing virtual world spaces to connect and share.

5 0
3 years ago
Other questions:
  • Gateway drugs are normally not addictive
    15·2 answers
  • If a driving who is under the age of 21 is stopping by a law enforcement officer and shown to here a bal of .02 or greater, he o
    14·1 answer
  • Xavier would like to change the sort options for his contacts. In which tab will he find the Arrangement command group?
    6·1 answer
  • The _________ in Java is passed by value but ______ is passedby reference.
    5·1 answer
  • What is the other name of the horizontal column graph
    9·1 answer
  • What is lasso tool write the name of any modelling and animation software<br>​
    8·1 answer
  • What is the awnser ?
    11·1 answer
  • What is string literal in Java?
    5·1 answer
  • A(n) ______ has moving mechanical parts, which makes it less reliable than solid-state drives, but its metal platters are sealed
    9·2 answers
  • Read-only memory chips are used to
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!