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
Memory of your familiar old e-mail password may block the recall of your new password. This illustrates
ikadub [295]

Answer:

Retroactive Interference

Explanation:

5 0
3 years ago
Hiding data within pictures, audio, or video files can be difficult to detect and find. What can be done to reduce this?
DIA [1.3K]

Answer:

The hiding of the information/data in the images ,videos and audio is referred as stenography This can reduced by taking several steps like:-

  • Extraction method is the widely used method for the separation and picking out the data and the image or other data forms from each other.
  • Hue and saturation method works on the images and audios that  can find the hidden data.
  • Image processing is doen on the image which enhances the form of it by extraction of the unnecessary data from it.

5 0
3 years ago
Write a static method named anglePairs that accepts three angles (integers), measured in degrees, as parameters and returns whet
padilas [110]

Answer:

The java program for the scenario is given below.  

import java.util.*;

public class Test

{

// method returning sum of all digits in the integer parameter

static boolean anglePairs(int a1, int a2, int a3)

{

   // variable to hold sum of two complementary angles

   int comp=90;  

   // variable to hold sum of two supplementary angles

   int supp=180;

   // boolean variable to hold result

   boolean result = false;

   if( (a1+a2 == comp) || (a2+a3 == comp) || (a1+a3 == comp) )

   {

       if( (a1+a2 == supp) || (a2+a3 == supp) || (a1+a3 == supp) )

           result = true;

       else

           result = false;

   }    

           // return variable result

       return result;

}

public static void main(String[] args)

{

   // method is called by passing three positive angles

   System.out.println("The pair of angles have both complementary and supplementary angles " + anglePairs(100, 80, 90) );    

}

}

OUTPUT

The pair of angles have both complementary and supplementary angles false

Explanation:

The program works as described below.

1. The method, anglePairs(), is declared as static having return type as Boolean which takes three positive integer parameters.

static boolean anglePairs(int a1, int a2, int a3)

{

}

2. The integer variable, comp, is declared and initialized to 90.

3. The integer variable, supp, is declared and initialized to 180.

4. The Boolean variable, result, is declared and initialized to false.

5. Using if-else statements, sum of two angles are compared with the variables, comp and supp.

6. If the sum of any two angles match with both, comp and supp, variables, the Boolean variable, result is assigned value true. Otherwise, result is assigned value false.

7. The value of result is returned.

8. Inside main() method, the function anglePairs() is called and three positive numerical values are passed as parameters.  The output is displayed.

9. All the above code is written inside the class Test.

10. User input is not taken since it is not mentioned in the question.

8 0
4 years ago
Try my game? https://scratch.mit.edu/projects/589314237
Mama L [17]

Answer:

ok :)

Explanation:

3 0
3 years ago
Read 2 more answers
I need help here thanks forever who helps
lubasha [3.4K]

Answer:

the horse, the man, and the cactus

Explanation:

The horse is running

The man got slung off the horse, so its in motion

The cactus is flying everywhere from the horse.

3 0
2 years ago
Other questions:
  • What device consisting of a screen and keyboard was used to connect to a mainframe computer?
    6·1 answer
  • When a packet with the code is transmitted, it is received and processed by every machine on the network. this mode of operation
    12·1 answer
  • What are the features of the Outline view in Word? Select three options.
    14·2 answers
  • Convert 1001101012 to base 10
    5·1 answer
  • Simon is producing a report on major rivers in North America. In the report he wants to add a table that displays the statistics
    12·1 answer
  • which of the following are available options when importing a photoshop document into adobe illustrator
    7·1 answer
  • What is dialog box? ​
    7·1 answer
  • In the context of applications of artificial intelligence (AI), _____ perform well at simple, repetitive tasks and can be used t
    11·1 answer
  • What is an a free open source content management system for publishing web content​
    8·1 answer
  • Who is known as the first computer programmer?​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!