User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,921 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,688 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Apr 2nd, 2005
Views: 11,361
A simple program to count the words, lines and sentences contained in a text file. The assumptions are made that words are separated by whitespaces, and sentences end with a period, question mark or exclamation mark.
python Syntax | 5 stars
  1. # count lines, sentences, and words of a text file
  2.  
  3. # set all the counters to zero
  4. lines, blanklines, sentences, words = 0, 0, 0, 0
  5.  
  6. print '-' * 50
  7.  
  8. try:
  9. # use a text file you have, or google for this one ...
  10. filename = 'GettysburgAddress.txt'
  11. textf = open(filename, 'r')
  12. except IOError:
  13. print 'Cannot open file %s for reading' % filename
  14. import sys
  15. sys.exit(0)
  16.  
  17. # reads one line at a time
  18. for line in textf:
  19. print line, # test
  20. lines += 1
  21.  
  22. if line.startswith('\n'):
  23. blanklines += 1
  24. else:
  25. # assume that each sentence ends with . or ! or ?
  26. # so simply count these characters
  27. sentences += line.count('.') + line.count('!') + line.count('?')
  28.  
  29. # create a list of words
  30. # use None to split at any whitespace regardless of length
  31. # so for instance double space counts as one space
  32. tempwords = line.split(None)
  33. print tempwords # test
  34.  
  35. # word total count
  36. words += len(tempwords)
  37.  
  38.  
  39. textf.close()
  40.  
  41. print '-' * 50
  42. print "Lines : ", lines
  43. print "Blank lines: ", blanklines
  44. print "Sentences : ", sentences
  45. print "Words : ", words
  46.  
  47. # optional console wait for keypress
  48. from msvcrt import getch
  49. getch()
  50.  
  51.  
Comments (Newest First)
vegaseat | Kickbutt Moderator | Jul 5th, 2005
This code is most likely more portable:
# optional console wait for keypress
raw_input('Press Enter...')
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 8:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC