I would like some advice as to how to write my wc program. Program should accept a file name as input and then print three numbers showing the count of lines, words and characters in the file. Chapter 4 question 15 of zelles introduction to python

Assuming the question does not want a count of unique words/characters this should do it:

open the file for reading
number of words,characters, lines = 0
while a line can be read from the file  read a line into a string
         number of lines + = 1
         number of characters + = length of string
         number of words + =  length of string split into a list of words 
print out number of words, lines, characters

Obviously this is not python but pseudo code that will very easily translate into python - I'll leave that to you. Look into string functions for splitting strings into a list of words (hint). The length of this list will give you the number of words. The length of a string is the number of characters and the number of line reads is the number of lines.

Thanks bgeddy. I really do not have much clue as to what I am doing but I am very grateful for your response. Do I type the toggle as written? What am I expected to substitute in the toggle?

Huglybits.

Program should accept a file name as input

Do you want the program to ask the user to key in a file name, or do you want to provide it as an argument when the program is called, or do you want to use a GUI that allows the user to browse the file system and choose a file?

I believe I want to use a GUI that allows the user to browse the file system and choose a file or to ask the user to key in a file name. I got the file which is saved in a .txt file. bgeddy used some + signs in his toggle please tell me what to do. Thanks woooee!

Sorry for not getting back sooner but it seems other kind folks in the forum have answered already.
It's hard to gauge someones level of experience when answering a question but I only outlined the program's logic broadly.

I recommend studying a beginners tutorial on python and playing around in the interpreter, following examples etc to get a feel for things.

Personally, if you are just starting out with programming in general, I wouldn't recommend developing gui front ends just yet. Perhaps just "raw_input() to read input from the user and "print " to output it.

Please don't take offence if I underestimate your level of experience and don't think I'm being deliberately vague in offering a solution. If I were to simply give you python code for a complete working program I don't think you'd learn as much as doing it for yourself. If this is a homework assignment, simply copying an offered solution verbatim will give a false impression of your proficiency.

Good luck with python !

Thats right! I will try to understand this, it is not all complicated. Thanks for all your help!

hi
I tried to the program mentioned above ,but my problem is I can get them done as three separate programs instead of one.Can anyone help me out?

I asked this question to someone back when I was coding my text editor(horrible project). THis is what that person gave me to use.

def count_words(self):
        text = self.text.get(1.0, 'end')
        words = len(text.split())
        print words
        self.master.title("Text has %d words" % words)

ITs a function that counts words. Hope this helps!

This article has been dead for over six months. Start a new discussion instead.