Question

PYTHON, write a function that accepts a text file's name and returns a. the number of...

PYTHON,

write a function that accepts a text file's name and returns

a. the number of uppercase letters in the file

b. the number of lowercase letters in the file

c. the number digits in file

d. the number of whitespace characters in the file

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def file_stats(filename):
    upper, lower, digits, whitespace = 0, 0, 0, 0
    try:
        with open(filename, 'r') as f:
            data = f.read()
            for ch in data:
                if ch.islower():
                    lower += 1
                elif ch.isupper():
                    upper += 1
                elif ch.isdigit():
                    digits += 1
                elif ch.isspace():
                    whitespace += 1
    except FileNotFoundError:
        pass
    return upper, lower, digits, whitespace


print(file_stats('input.txt'))
Add a comment
Know the answer?
Add Answer to:
PYTHON, write a function that accepts a text file's name and returns a. the number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python question! points) Attached to the Exam #4 code Assignment in Tracs you ## will find...

    python question! points) Attached to the Exam #4 code Assignment in Tracs you ## will find a file, "text.txt". Text is stored 1 sentence per line. ## Write a program below that reads the file contents and displays ## the following: ## 1. Number of uppercase characters in the file ## 2. Number of lowercase characters in the file ## 3. Number of digits in the file. ## 4. Number of whitespace characters in the file ## ## print("Number of...

  • i need the answer for this python question using dictionary method please!!! points) Attached to the...

    i need the answer for this python question using dictionary method please!!! points) Attached to the Exam #4 code Assignment in Tracs you ## will find a file, "text.txt". Text is stored 1 sentence per line. ## Write a program below that reads the file contents and displays ## the following: 1. Number of uppercase characters in the file 2. Number of lowercase characters in the file 3. Number of digits in the file. 4. Number of whitespace characters in...

  • You're to write a C++ program that analyzes the contents of an external file called data.txt....

    You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to...

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a Python...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a Python code that gets a string from the user and counts the number of uppercase letters, the number of lowercase letters, the number of digits, and the sum of the digits. Please see the outcome below: Outcome: Enter the string: okOKE9o7 Uppercase letters: 3 Lowercase letters: 3 Digits: 2 Sum of digits: 8 Must look exactly like outcome for full credit.

  • 1. Write a Python function that accepts a string and calculate the number of upper case...

    1. Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters. Sample String : 'The quick Brow Fox' Expected Output : No. of Upper case characters : 3 No. of Lower case Characters : 12

  • 5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename),...

    5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename), starting line number i.e. m) and ending line number (i.e. n) as a parameter. The function then returns the contents from line number m to n (m <n). If m < 1 then start from line 1. Similarly, if n > number of lines the file, then stop at the last line in the file. Sample Input: readlinesmton("data.txt",5,7) Sample Input: readlinesmton("data.txt", 0,10)

  • PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns...

    PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. Test case: the arguments are “apple@123” and “banana@#345”, your program should return “a@3”. Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. (20 pts) Test case: the arguments are "apple@123” and “banana@#345”, your program should return "a@3".

  • Write a javascript function that accepts 3 arrays and returns a string of the elements sorted...

    Write a javascript function that accepts 3 arrays and returns a string of the elements sorted in ascending value, with duplicates removed. Return all alphabetic characters in lowercase. E.G. function([ 1,2,3, d], [5, 3, 0, ‘a’], [‘a’,’d’,9]) returns “0,1,2,3,5,9,a,d” E.G. function(['w', 'i', 'H', 5], [8, 5, 9, 'c', 'Z'], [6, 4, 'a', 'b', 'y']) returns “4,5,6,8,9,a,b,c,h,i,w,y,z” we must not use ' sort, remove, split,join and any built-in fucntions

  • In Java code Write a complete method name convertFile that accepts a File object representing an...

    In Java code Write a complete method name convertFile that accepts a File object representing an input text file and a PrintWriter objects as arguments. The method should read the contents of the input file and change all characters to uppercase and store the results in the output file.

  • For this exercise code a program in python which will read the text from the file...

    For this exercise code a program in python which will read the text from the file in.txt. Then this program converts all the lowercase letters to the corresponding uppercase letters. The results will be written to the file out.txt. The ide for python i am using is python 3.7 input: it will be cold as hell in the winter output: IT WILL BE COLD AS HELL IN THE WINTER

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT