Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:48:27+00:00 2026-06-01T04:48:27+00:00

I’m trying to write a script to pull the word count of many files

  • 0

I’m trying to write a script to pull the word count of many files within a directory. I have it working fairly close to what I want, but there is one part that is throwing me off. The code so far is:

import glob

directory = "/Users/.../.../files/*"
output = "/Users/.../.../output.txt"

filepath = glob.glob(directory)

def wordCount(filepath):
    for file in filepath:
        name = file
        fileO = open(file, 'r')
        for line in fileO:
            sentences = 0
            sentences += line.count('.') + line.count('!') + line.count('?')

            tempwords = line.split()
            words = 0
            words += len(tempwords)

            outputO = open(output, "a")
            outputO.write("Name: " + name + "\n" + "Words: " + str(words) + "\n")

wordCount(filepath)

This writes the word counts to a file named “output.txt” and gives me output that looks like this:

Name: /Users/..../..../files/Bush1989.02.9.txt
Words: 10
Name: /Users/..../..../files/Bush1989.02.9.txt
Words: 0
Name: /Users/..../..../files/Bush1989.02.9.txt
Words: 3
Name: /Users/..../..../files/Bush1989.02.9.txt
Words: 0
Name: /Users/..../..../files/Bush1989.02.9.txt
Words: 4821

And this repeats for each file in the directory. As you can see, it gives me multiple counts for each file. The files are formatted such as:

Address on Administration Goals Before a Joint Session of Congress

February 9, 1989

Mr. Speaker, Mr. President, and distinguished Members of the House and
Senate…

So, it seems that the script is giving me a count of each “part” of the file, such as the 10 words on the first line, 0 on the line break, 3 on the next, 0 on the next, and then the count for the body of the text.

What I’m looking for is a single count for each file. Any help/direction is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:48:29+00:00Added an answer on June 1, 2026 at 4:48 am

    The last two lines of your inner loop, which print out the filename and word count, should be part of the outer loop, not the inner loop – as it is, they’re being run once per line.

    You’re also resetting the sentence and word counts for each line – these should be in the outer loop, before the start of the inner loop.

    Here’s what your code should look like after the changes:

    import glob
    
    directory = "/Users/.../.../files/*"
    output = "/Users/.../.../output.txt"
    
    filepath = glob.glob(directory)
    
    def wordCount(filepath):
        for file in filepath:
            name = file
            fileO = open(file, 'r')
            sentences = 0
            words = 0
            for line in fileO:
                sentences += line.count('.') + line.count('!') + line.count('?')
    
                tempwords = line.split()
                words += len(tempwords)
    
            outputO = open(output, "a")
            outputO.write("Name: " + name + "\n" + "Words: " + str(words) + "\n")
    
    wordCount(filepath)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.