473,473 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

i need help asap

3 New Member
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
4 1674
bartonc
6,596 Recognized Expert Expert
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
briggsie2006
3 New Member
what about chacters?
Oct 4 '06 #3
briggsie2006
3 New Member
what about characters?
Oct 4 '06 #4
bartonc
6,596 Recognized Expert Expert
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

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

Similar topics

4
by: Philip D Heady | last post by:
I'm in need of a good PHP programmer to help me with my lodgings website asap. Someone who can work on it during the day for a few hrs while I'm at work would be great. Prefer they live in USA...
1
by: whisper | last post by:
Medium/small site with mod_python and sqllite or mySQL running Python 2.3.3 or later on Apache 2.x. I can do the python and CGI, but might need a _little_ hand holding for the rest. Will also...
2
by: ratedr1 | last post by:
I am currently trying to label rings (jewelry). They make specialized tags (shaped like a butterfly, barbell, dumbell, etc) for this purpose, the label goes through the ring, and then the ends...
5
by: Witless | last post by:
This is 'supposed' to be a simple problem and shouldn't need too much time to solve :S Below is a function that works like this: int fn(int input) { int x=(input/8);
2
by: Chad A. Beckner | last post by:
Hey all, I have a ASP .NET webpage which contains several panels. When I click a button on the first "panel" page, it postsback to the same page, and I switch the visibility of the panels...
1
by: Massimo Bonanni | last post by:
Hi, I try to implement ASAP protocol in my web service, but I find a very hard problem. I define my SOAP Header: public class Request : SoapHeader {
0
by: SQLJunkie | last post by:
Please refer to the following URL for more info... ...
5
by: Karthik D V | last post by:
Hello All, I have a table like this ID CHARACTER ----------- --------- 1 A 2 A 3 B 4 B
5
by: byrnes | last post by:
We are looking to immediately recruit programmers for a project; we are interested in obtaining proposals ASAP. Will offer favorable compensation. We need immediately, an individual to complete...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.