0

How to have python read a text file and return the number of lines, characters, vowels, consonants, lowercase letters, and uppercase letters

Write a program that accepts the name of a file as a command-line argument. (You can assume that the input file will be a plain-text file.) If the user forgets to include the command line argument, your program should exit with an appropriate error message.

Otherwise, your program should print out:

  • The number of lines in the file
  • The number of characters in the file
  • The number of vowels in the file (For the purposes of this assignment, treat "y" as a consonant.)
  • The number of consonants in the file
  • The number of lowercase letters in the file
  • The number of uppercase letters in the file

I am at a lose. How would I do this? like im pretty sure there are commands that can do this but I dont know what they are. Thanks for your help :)

EDIT This is my final program and its perfect. Thank you all for your help. Special thanks to Bentaye :)

import sys
def text(): 
    countV = 0 
    countC = 0 
    lines = 0 
    countU = 0
    countL = 0
    characters = 0 
    vowels = set("AEIOUaeiou") 
    cons = set("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ")    
    upper = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    lower = set("abcdefghijklmnopqrstuvwxyz")

    with open(sys.argv[1]) as file:
        fileLines = file.readlines()

    for line in fileLines: 
        lines = lines + 1 
        characters = characters + len(line) 
        for char in line: 
            if char in vowels: 
                countV = countV + 1 
            elif char in cons: 
                countC = countC + 1
        for char in line:
            if char in upper:
                countU = countU + 1
            elif char in lower:
                countL = countL + 1
    print("Lines: " + str(lines)) 
    print("Characters: " + str(characters)) 
    print("Vowels: " + str(countV)) 
    print("Consonants: " + str(countC))
    print("Lowercase: " + str(countL))
    print("Uppercase: " + str(countU))
text()
Sam
  • 21
  • 2
  • 2
    Have you done any research on this? – L_Church Apr 11 '18 at 09:05
  • 2
    Read the file as a list of Strings (one per line) and iterate character by character and increment counters depending on each character. Try it, then update your question with your code, and we will help you. Start here: https://stackoverflow.com/questions/3277503/how-do-i-read-a-file-line-by-line-into-a-list – Bentaye Apr 11 '18 at 09:05
  • Use `argparse` to take command line argument, iterate over each character and do the processing. Write some code and if you are not able to we will help you. – Shashank Singh Apr 11 '18 at 09:10
  • @Sam I added it to your question as an Edit. You're pretty much there already – Bentaye Apr 11 '18 at 10:19
  • Thank you ive used this website to find others questions that are similar but ive never asked a question so I didnt know the procedure – Sam Apr 11 '18 at 10:21
  • Do you know why the vowels and constants wont work. they keep outputting 0 – Sam Apr 11 '18 at 10:22
  • @Sam See my answer below. And keep in mind that on StackOverflow you will only get help if you show you tried hard enough before asking. – Bentaye Apr 11 '18 at 10:29
  • And you didn't think it would be useful to read the [tour] when signing on either. – Jongware Apr 11 '18 at 10:29
  • Im a college student trying to get through finals I dont got that kinda time to read a tour. – Sam Apr 11 '18 at 10:35

1 Answers1

1

This fixes your problem, you can build onto it now for upper/lower cases

  • use sys.argv[0] to capture the argument (you need to import sys)
  • Then use file.readlines() to get an array of lines (as Strings)

Code

import sys

countV = 0 
countC = 0 
lines = 0 
characters = 0 
vowels = set("AEIOUaeiou") 
cons = set("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ") 

with open(sys.argv[0]) as file:
    fileLines = file.readlines()

    for line in fileLines: 
        lines = lines + 1 
        characters = characters + len(line) 
        for char in line: 
            if char in vowels: 
                countV = countV + 1 
            elif char in cons: 
                countC = countC + 1

    print("Lines: " + str(lines)) 
    print("Characters: " + str(characters)) 
    print (countV) 
    print (countC)

You call it this way

python test.py yourFile.txt 

Complete answer for reference

import sys

vowels = "aeiou"
cons = "bcdfghjklmnpqrstvwxyz"

with open(sys.argv[0]) as file:
    fileLines = file.readlines()

    countVowels = 0 
    countConsonants = 0 
    countUpperCase = 0 
    countLowerCase = 0 
    countLines = 0 
    countCharacters = 0 
    countNonLetters = 0 

    for line in fileLines: 
        countLines += 1 
        countCharacters = countCharacters + len(line) 
        for char in line: 
            if char.isalpha():
                if char.lower() in vowels: 
                    countVowels += 1 
                elif char.lower() in cons: 
                    countConsonants += 1

                if char.isupper():
                    countUpperCase += 1
                elif char.islower():
                    countLowerCase += 1

            else:
                countNonLetters += 1

    print("Lines: " + str(countLines)) 
    print("Characters: " + str(countCharacters)) 
    print("Vowels: " + str(countVowels)) 
    print("Consonants: " + str(countConsonants)) 
    print("Upper case: " + str(countUpperCase)) 
    print("Lower case: " + str(countLowerCase)) 
    print("Non letters: " + str(countNonLetters)) 
Bentaye
  • 9,403
  • 5
  • 32
  • 45
  • Thank You so much I will let you know if I need further help. Is there anything I can do to boost your credit on this website? – Sam Apr 11 '18 at 10:31
  • @Sam you can upvote the answer too :) Comment here if you need some more help with it. Also not sure how you want to deal with spaces and other special characters (parenthesis, commas, etc), I guess they don't count as lower case. Be aware of that – Bentaye Apr 11 '18 at 10:32