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
schepotkina [342]
3 years ago
7

Write a function named file_stats that takes one string parameter (in_file) that is the name of an existing text file. The funct

ion file_stats should calculate three statistics about in_file i.e. the number of lines it contains, the number of words and the number of characters, and print the three statistics on separate lines.
For example, the following would be be correct input and output. (Hint: the number of characters may vary depending on what platform you are working.)
>>> file_stats('created_equal.txt')

lines 2
words 13
characters 72

Computers and Technology
1 answer:
Ivan3 years ago
6 0

Answer:

Here is the Python program.

characters = 0

words = 0

lines = 0

def file_stats(in_file):

   global lines, words, characters

   with open(in_file, 'r') as file:

       for line in file:

           lines = lines + 1

           totalwords = line.split()

           words = words + len(totalwords)

           for word in totalwords:

               characters= characters + len(word)

file_stats('created_equal.txt')

print("Number of lines: {0}".format(lines))

print("Number of words: {0}".format(words))

print("Number of chars: {0}".format(characters))

   

Explanation:

The program first initializes three variables to 0 which are: words, lines and characters.

The method file_stats() takes in_file as a parameter. in_file is the name of the existing text file.

In this method the keyword global is used to read and modify the variables words, lines and characters inside the function.

open() function is used to open the file. It has two parameters: file name and mode 'r' which represents the mode of the file to be opened. Here 'r' means the file is to be opened in read mode.

For loop is used which moves through each line in the text file and counts the number of lines by incrementing the line variable by 1, each time it reads the line.

split() function is used to split the each line string into a list. This split is stored in totalwords.

Next statement words = words + len(totalwords)  is used to find the number of words in the text file. When the lines are split in to a list then the length of each split is found by the len() function and added to the words variable in order to find the number of words in the text file.

Next, in order to find the number of characters, for loop is used. The loop moves through each word in the list totalwords and split each word in the totalwords list using split() method. This makes a list of each character in every word of the text file. This calculates the number of characters in each word. Each word split is added to the character and stored in character variable.

file_stats('created_equal.txt')  statement calls the file_stats() method and passes a file name of the text file created_equal.txt as an argument to this method. The last three print() statements display the number of lines, words and characters in the created_equal.txt text file.

The program along with its output is attached.

You might be interested in
How to type in color on clash royale
Brut [27]
In order to chat your text in color in the famous game Clash Royale of Supercell, you have to use a template below: 
<c + “hexadecimal value of the color of your choice.”> text here </c>.
For example, to make your text in red (ff0000) : <cff0000> text here </c>
8 0
3 years ago
Read 2 more answers
My friend has a battery of 9000mah and loses 10 every day how many days well it take to go to zero
forsale [732]
I think it's 900 but I can't be sure.
8 0
3 years ago
The pseudo-class selectors for links let you use CSS to change the formatting for all but one of the following. Which one is it?
aev [14]
B. A link that is inactive
7 0
3 years ago
What is the output of the given code?<br><br> A. Compiler error<br> B. 0505<br> C. Smooth<br> D. 0.0
oksano4ka [1.4K]

Answer:

The answer is A. Compile error

Explanation:

In the class Fabric, fabricID has private access. You can only print out out private variables in the class or function they are assigned in.

ps: sorry if I am wrong, I am kind of new to java

6 0
3 years ago
Why is it not advisable to mark tlc plates with a pen to indicate?
nikitadnepr [17]
The plate can change the function that is in it. If you were to name it wrong, you would have to debug the code and possibly rewrite it.
3 0
4 years ago
Other questions:
  • Is the protocol that specifies how web browsers and servers communicate.?
    11·1 answer
  • Which of the following are personal video journal entries posted on the web? Select one: A. Podcasts B. Vlogs C. Blogs D. Newsgr
    14·1 answer
  • Compare and contrast fair use to copyright.
    5·1 answer
  • All of the following methods ensure the stored data are unreadable except…?
    7·1 answer
  • What types of storage can be used to access your data on another computer?
    7·2 answers
  • A programmer writing code in class Point attempts to override the following inherited method:public boolean equals( Object o ) {
    12·1 answer
  • who will follow me on tiktok and like all my videos? if you do ill give branlist and give u a shoutout and you can enter my big
    15·1 answer
  • why when I click apostrophe I get an accent mark? And when I try quotation marks I get this ( ¨ ) how do I change that or why is
    13·1 answer
  • Windows OS and Mac OS are examples of which type of operating systems?
    5·1 answer
  • 5. The image file format most suited for photographs is what? *
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!