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

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2
    infile = open ( fname, 'r' )
    fcontents = infile.read()
    fcontents2 = infile.readlines()

You cannot read from a file twice.

4 Comments

You could also reposition the cursor with seek(), could you not?
@sr2222 - yes, but you have to do it. Otherwise it is better to read the file once and then split it according to needs.
You might want to suggest fcontents2 = fcontents.splitlines() or something to that effect.
@eumiro Granted. Just pointing out that 'cannot' is not, strictly speaking, true.
0

When you read from a file, the file handle remembers its position in the file. Thus, after your call infile.read(), infile will be placed at the end of the file. When you then call infile.readlines(), it will try to read all the characters between its current position and the end of the file, and hence return an empty list.

You can rewind the file to its initial position using infile.seek(0). Thus:

>>> fcontents = infile.read()
>>> infile.seek(0)
>>> flines = infile.readlines()

will work.

Alternatively, having read the file into the string fcontents, you can split the string into lines using splitlines:

>>> fcontents = infile.read()
>>> flines = fcontents.splitlines()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.