Computer Science, asked by suniltty180, 4 months ago

3.Write a python program to read a file named "article.txt", count and print the following:

(i). length of the file(total characters in file)

(ii).total alphabets

(iii). total upper case alphabets

(iv). total lower case alphabets

(v). total digits (vi). total spaces

(vii). total special characters

please give only right answer​

Answers

Answered by greeshmakachirayil
1

Answer:

The code for the given question is:

article_file = open("article.txt", "r+")

text = article_file.read()

count = 0

space = 0

upper = 0

lower = 0

digits = 0

special_char = 0

alphabets = 0

for i in text:

  if i != "":

      count += 1

  elif i.isalpha():

      alphabets += 1

      if i.isupper():

          upper += 1

      if i.islower():

          lower += 1

  elif i.isdigit():

      digits += 1

  elif i.isspace():

      space += 1

  else:

      special_char += 1

article_file.close()

print(f"Total Characters: {count}\nSpaces: {space}\nAlphabets: {alphabets}\n Uppercase Characters: {upper}\nLowercase Characters: {lower}\nDigits: {digits}\nSpecial Characters: {special_char}")

File creation, writing, and reading functions are built into Python. Normal text files and binary files are the two types of files that may be handled in Python.

Text files: In these types of files, each line of text is ended by a unique character called EOL (End of Line), which in Python is equivalent to the new line character ('n').

The data is saved after being translated into the machine-understandable binary language in binary files, which lack a line terminator.

Python's count() function for Lists returns the number of times an object appears in a List.

Exception:

If more than one parameter is supplied to the count() function, a TypeError is raised.

Learn more about data types here:

https://brainly.in/question/28021729

Learn more about print in python here:

https://brainly.in/question/5558161


#SPJ3

Similar questions