By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,402 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,402 IT Pros & Developers. It's quick & easy.

i need help asap

P: 3
I need to write a program called wc.py. It promts the user to input a filename and outputs the number of lines, words, and, characters in the file. I really need help on this because I don't know what to do. email me at briggsie2006@hotmail.com asap if you can help me out.
Oct 3 '06 #1
Share this Question
Share on Google+
4 Replies


bartonc
Expert 5K+
P: 6,596
Expand|Select|Wrap|Line Numbers
  1. ### We'll use Tk to make a window with a button so that we can get
  2. ### a file dialog box. You can add some place to display the results. It's
  3. ### probably a lot for starting out, but seeing this stuff now will
  4. ### give you a big head start!
  5.  
  6. import Tkinter as Tk
  7. import tkFileDialog
  8.  
  9. ### Use tkFileDialog.askopenfilename() to get the file name
  10. ### because it's just too hard for people to type in file names
  11.  
  12. def DoEverything():
  13.     # initialize some variable for your counters
  14.     nLines = 0
  15.     nWords = 0
  16.     # get the file name
  17.     fileName = tkFileDialog.askopenfilename()
  18.  
  19.     # Once you have the name, call built-in open(); 'r' is for read mode.
  20.     theFileObject = open(fileName, 'r')
  21.     # File objects are of a class that support itteration, so you use a for loop
  22.     # to get a string object which is a copy of the line in the file
  23.     # I always call this variable 'line'
  24.     for line in theFileObject:
  25.         nLines += 1     # add one to the counter each time through the loop
  26.         listOfWords = line.split()  # string objects have lots of "methods"
  27.         print listOfWords    # Use print to see what your program is doing
  28.         wordCount = len(listOfWords)    # Use extra variable to simplify your code
  29.         print wordCount
  30.         nWords += wordCount # Add this lines word count to nWords
  31.  
  32.     # There are simpler ways to print, but format strings are a really good thing to learn
  33.     print "%s has %d lines and %d words" %(fileName, nLines, nWords)
  34.     # although Python will close the file if you forget, it's always good to remember
  35.     theFileObject.close()
  36.  
  37.  
  38. if __name__ == "__main__":
  39.  
  40.     ### This is all it takes to start a Graphical User Interface with Tkinter!
  41.  
  42.     root = Tk.Tk()
  43.     mainFrame = Tk.Frame(root)
  44.     mainFrame.pack()
  45.     fdButton = Tk.Button(mainFrame, text="File Dialog", command=DoEverything)
  46.     fdButton.pack()
  47.  
  48.  
  49.     root.mainloop()
  50.  
Oct 4 '06 #2

P: 3
what about chacters?
Oct 4 '06 #3

P: 3
what about characters?
Oct 4 '06 #4

bartonc
Expert 5K+
P: 6,596
what about characters?
Here's a big hint:

You'll need one more counter variable
Use a for loop inside the main loop
list objects (in this case listOfWords) are itterators, so use

Expand|Select|Wrap|Line Numbers
  1. for item in listOfWords:
  2.     # use len() to count characters
  3.     # accumulate total count
  4.  
I'll bet you get it!
Post what you come up with...
Oct 4 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.