Write a program to count the number of uppercase alphabets present in a text file Poem txt

This is a Python Program to count the number of words in a text file.

Problem Description

The program takes the file name from the user and counts number of words in that file.

Problem Solution

1. Take the file name from the user.
2. Read each line from the file and split the line to form a list of words.
3. Find the length of items in the list and print it.
4. Exit.

Program/Source Code

Here is source code of the Python Program to count the number of words in a text file. The program output is also shown below.

fname = input("Enter file name: ")
 
num_words = 0
 
with open(fname, 'r') as f:
    for line in f:
        words = line.split()
        num_words += len(words)
print("Number of words:")
print(num_words)

Program Explanation

1. User must enter a file name.
2. The file is opened using the open() function in the read mode.
3. A for loop is used to read through each line in the file.
4. Each line is split into a list of words using split().
5. The number of words in each line is counted using len() and the count variable is incremented.
6. The number of words in the file is printed.

Runtime Test Cases

 
Case 1:
Contents of file: 
Hello world
 
Output: 
Enter file name: data1.txt
Number of words:
2
 
Case 2:
Contents of file: 
This programming language is
Python
 
Output: 
Enter file name: data2.txt
Number of words:
5

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Write a program to count the number of uppercase alphabets present in a text file Poem txt

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

Type A: Short Answer Questions / Conceptual Questions

  1. What is difference between "w" and "a" modes?

  2. What is significance of file-object?

  3. How is file open() function different from close() function?

  4. Write a statement to open a binary file C:\Myfiles\Text1.txt in read and write mode by specifying the file path in two different formats.

  5. When a file is opened for output, what happens when

  6. What role is played by file modes in file operations? Describe the various file mode constants and their meanings.

  7. What are the advantages of saving data in:

  8. When do you think text file should be preferred over binary files?

  9. Write a statement in Python to perform the following operations:

Type B: Application Based Questions

  1. How are the following codes different from one another?

  2. If the file 'poemBTH.txt' contains the following poem (by Paramhans Yoganand):

  3. Consider the file poemBTH.txt given above (in previous questions). What output will be produced by following code fragment?

  4. Consider the file "poemBTH.txt" and predict the output of following code fragments if the file has been opened in filepointer file1 with code:

  5. What is following code doing?

  6. Write code to open file created in previous question and print it in following form:

  7. Consider the file "contacts.csv" created in above question and figure out what the following code is trying to do?

  8. Consider the file poemBTH.txt and predict the output of following code fragment. What exactly is following code fragment doing?

  9. If you use the code of Q.8 with p1.txt created in solved problem 14, what would be its output?

  10. Write a method in python to read the content from a text file diary.txt line by line and display the same on screen.

  11. Write a method in python to write multiple line of text content into a text file mylife.txt.line.

Type C: Programming Practice / Knowledge based Questions

  1. Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space.

  2. A file sports.dat contains information in following format:

  3. A file contains a list of telephone numbers in the following form:

  4. Write a program to count the words "to" and "the" present in text file "Poem.txt".

  5. Write a program to count the number of upper-case alphabets present in a text file "Article.txt".

  6. Write a program that copies one file into another. Have the program read the file names from user?

  7. Write a program that appends the contents of one file to another. Have the program take the filenames from the user.

  8. Write a program that reads characters from the keyboard one by one. All lower case characters get stored inside the file LOWER, all upper case characters get stored inside the file UPPER and all other characters get stored inside file OTHERS.

  9. Write a function in Python to count and display the number of lines starting with alphabet 'A' present in a text file "LINES.TXT". e.g., the file "LINES.TXT" contains the following lines: