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

  • Home
  • SEARCH
  • 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 8482827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:54:59+00:00 2026-06-10T19:54:59+00:00

i needed to create a program that would read a text file and count

  • 0

i needed to create a program that would read a text file and count the number of lines, words and characters. I got it all working below if seperated individually but i wanted to convert it into using functions so it can read the file once, but i keep getting different answers and unsure what im doing wrong.

Words Code

print ' '
fname = "question2.txt"
infile = open ( fname, 'r' )
fcontents = infile.read()

words = fcontents.split()
cwords = len(words)

print "Words: ",cwords

Characters Code

fname = "question2.txt"
infile = open ( fname, 'r' )
fcontents = infile.read()

char = len(fcontents)

print "Characters: ", char

Lines Code

fname = "question2.txt"
infile = open ( fname, 'r' )
fcontents = infile.readlines()

lines = len(fcontents)

print "Lines: ", lines

Correct Results

Words: 87
Characters: 559
Lines: 12

This is what I came up while trying to use functions but just cant figure out what’s wrong.

def filereader():
    fname = 'question2.txt'
    infile = open ( fname, 'r' )
    fcontents = infile.read()
    fcontents2 = infile.readlines()

    return fname, infile, fcontents, fcontents2


def wordcount(fcontents):
    words = fcontents.split(fcontents)
    cwords = len(words)
    return cwords

def charcount(fcontents):
    char = len(fcontents)
    return char

def linecount(fcontents2):
    lines = len(fcontents2)
    return lines


def main():

    print "Words: ", wordcount ('cwords')
    print "Character: ", charcount ('char')
    print "Lines: ", linecount ('lines')

main()

Wrong Results

Words: 2
Character: 4
Lines: 5

  • 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-10T19:55:00+00:00Added an answer on June 10, 2026 at 7:55 pm

    You need to use filereader in main:

    def main():
        fname, infile, fcontents, fcontents2 = filereader()
        print "Words: ", wordcount (fcontents)
        print "Character: ", charcount (fcontents)
        print "Lines: ", linecount (fcontents2)
    

    Otherwise, how would you obtain the values for fcontents and fcontents2 to pass to your other functions? You also need to fix filereader to make sure it will read the file once:

    def filereader():
        fname = 'question2.txt'
        infile = open ( fname, 'r' )
        fcontents = infile.read()
        fcontents2 = fcontents.splitlines(True)
        return fname, infile, fcontents, fcontents2
    

    Note that the line for fcontents2 has been modified to split fcontents on newlines (see str.splitlines). This will also gives you a list of strings as .readlines() would do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

which framework helps in reducing the number of lines of code needed to create
I have a program that reads a huge text file (line by line) and
I'm reaaallly new at JSON, but here's what I got. I needed to create
Needed to write a server text file as the output of a business process
I'm writing a small program to read in a csv with a variable number
As a school assignment, I am supposed to create a program in C# that
I would like to implement a number of functions in (32bit) NASM that have
for a college exercise that I've already submitted I needed to read a .txt
I've been having some problems with a program that I've been writing and would
I have been trying to create a Main program that calls another program, sending

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.