-2

I am trying to make a word counter program incorporating easygui for file selection. I currently use Eclipse SDK with PyDev plugin (If there's any recommendations for a better Python environment).

Here is my code in its current state:

#This program is supposed to take a word file, and count the amount of lines and
#words. If the entered file is not a .txt, .doc, or .docx, then the program will
#ask for a different file.

from easygui import fileopenbox

filename = fileopenbox()
lines, words = 0, 0

#This method will count the amount of lines and words in a program and display
#it to the user
def word_count():
    if filename.endswith('.docx'): #If the file extension is .docx
        print("Your file has" + num_words + "words") #Print the amount of lines and words in the file.
    elif filename.endswith('.doc'): #If the file extension is .doc
        #<CODE WHICH COUNTS LINES AND WORDS>
        print("Your file has", lines, "lines, and" ,"words") #Print the amount of lines and words in the file.
    elif filename.endswith('.txt'): #If the file extension is .txt
        #<CODE WHICH COUNTS LINES AND WORDS>
        print("Your file has", lines, "lines, and" ,"words") #Print the amount of lines and words in the file.
    elif filename.endswith('.py'): #If the file extension is .py
        #<CODE WHICH COUNTS LINES AND WORDS>
        print("Your file has", lines, "lines, and" ,"words") #Print the amount of lines and words in the file.
    elif filename.endswith('.java'): #If the file extension is .java
        #<CODE WHICH COUNTS LINES AND WORDS>
        print("Your file has", lines, "lines, and" ,"words") #Print the amount of lines and words in the file.
    else:
        print("Are you trying to annoy me? How about giving me a TEXT or SOURCE CODE file, genius?")#Print an insulting error message.

As the code shows, I want the program to read the file extension, and if it matches any of those, run the word count code. However, my question is, what is that word count code? it seems like the use of fileopenbox() from easygui will make things even more complicated. any helps are appreciated thanks in advance

poke
  • 369,085
  • 72
  • 557
  • 602
1437
  • 13
  • 5

1 Answers1

1
from easygui import fileopenbox

def word_count(filename):
    if not filename.endswith(('.txt', '.py', '.java')):
        print('Are you trying to annoy me? How about giving me a TEXT or SOURCE CODE file, genius?')
        return

    with open(filename) as f:
        n_lines = 0
        n_words = 0
        for line in f:
            n_lines += 1
            n_words += len(line.split())
    print('Your file has {} lines, and {} words'.format(n_lines, n_words))

filename = fileopenbox()
word_count(filename)
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Really appreciate the answer falsetru. It compiles perfectly, but when I run and select the file, I recieve this: Traceback (most recent call last): File "C:\Users\Vonologic\workspace\Word Counter\main.py", line 23, in word_count(filename) TypeError: word_count() takes 0 positional arguments but 1 was given – 1437 Jun 15 '13 at 12:18
  • Actually, the OP wants to know "what is that word count code". Your answer shows how it might be done for plain text files, but does so unconditionally, not even differentiating with `if/elf`s on the extension like the OP's did. Sorry, no cigar. – martineau Jun 15 '13 at 12:19
  • @DavonG, pass filename to word_count: `word_count(filename)`. – falsetru Jun 15 '13 at 15:33
  • @DavonG: The code in this answer is probably OK for the last three file extensions because they're plain text. For MS-Word documents you're going to need a tool that can parse the file format. There are many questions here on SO about doing that. Take a [look](http://stackoverflow.com/search?q=[python]+read+word+doc). – martineau Jun 15 '13 at 16:04
  • @falsetru: You probably don't want `'rb'` for plain text. In fact on Windows you might need to specify `'rt'`. The UnicodeDecodeError is probably because the OP tried it with a non-plain text file. – martineau Jun 15 '13 at 16:11
  • @martineau, Thank you for your advice. I excluded .docx, .doc, and revert 'rb' mode to 'r'. – falsetru Jun 15 '13 at 16:14
  • @DavonG: The answer to [Is it possible to read Word files (.doc/.docx) in Python](http://stackoverflow.com/questions/16516044/is-it-possible-to-read-word-files-doc-docx-in-python) looks like a good reference. – martineau Jun 15 '13 at 16:14
  • @falsetru Also "wc += len(line.split())" wc was never declared before that, and I'm getting an error because of it. I set it to 0, but I got a UnicodeDecodeError afterwards. – 1437 Jun 15 '13 at 17:20
  • @DavonG, Oops, I fixed that. Try run the script in console (not in pydev). If UnicodeDecodeError still exist, please change `open(filename)` to `open(filename, 'rb')`, and retry. – falsetru Jun 15 '13 at 17:23
  • @DavonG, No, not in IDLE. In `cmd.exe`. – falsetru Jun 15 '13 at 17:32
  • @falsetru I'm supposed to just write the code in the command prompt? Without indentations and formatting...? – 1437 Jun 15 '13 at 17:35
  • @DavonG, Save your script as `wc.py` or some other name you like. Then open cmd.exe, run the script: `python wc.py`. – falsetru Jun 15 '13 at 17:39
  • Where should I place it? CMD can't seem to find the script – 1437 Jun 15 '13 at 17:41
  • I get UnicodeDecodeErrors with and without the RB. Just for confirmation, did I do this right? http://i.imgur.com/rxRmYdD.png – 1437 Jun 15 '13 at 17:48
  • Update: Never mind, it works perfectly. I was using a text file with a bunch of weird characters, which is why it didn't work before. – 1437 Jun 15 '13 at 18:31