user1649283
user1649283

Reputation: 3

Using Functions In Python

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

Upvotes: 0

Views: 243

Answers (3)

Simeon Visser
Simeon Visser

Reputation: 122336

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.

Upvotes: 2

Pascal Bugnion
Pascal Bugnion

Reputation: 4928

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()

Upvotes: 0

eumiro
eumiro

Reputation: 212835

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

You cannot read from a file twice.

Upvotes: 2

Related Questions