1

This is what I have (which includes extra processing):

import string

def main():
    fileInput = open("A tisket a tasket.txt",'r')
    characters, words, lines = countEmUp(fileInput)
    printCounts(characters, words, lines)
    tokenList = splitToTokens(fileInput)
    print(tokenList)

def countEmUp(someFileInput):
    lines, words, chars = 0, 0, 0
    for line in someFileInput:
        chars += len(line)  # using += for nice and concise updates
        words += len(line.split())
        lines += 1
    return chars, words, lines

def printCounts(ch,wd,ln):
    print("Your file contains the following:")
    print("Characters:",ch)
    print("Words:",wd)
    print("Lines:",ln)

def splitToTokens(aFileInput):
    tokenList = []
    for line in aFileInput:
        tokenList.extend(line.split())
    return tokenList

  main()

The problem starts at the splitToTokens function.

What I'm trying to do is create an empty list, iterate through a file I opened for reading line-by-line, and add the tokens in that line to my tokenList so I can process the tokens later.

When I print tokenList in my main() function it's still an empty list.

I suspect that maybe it has to do with my fileInput already being called? I'm trying not to open the file more than once.

q-compute
  • 641
  • 2
  • 7
  • 15
  • what's your `aFileInput` you pass in? – Tiny.D Nov 17 '17 at 03:35
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Nov 17 '17 at 03:36
  • show more of the code (i.e. where you create aFileInput) – Mohammed Elmahgiubi Nov 17 '17 at 03:37
  • I think the code you show is OK. I suspect the error is elsewhere. – John Anderson Nov 17 '17 at 03:40
  • @JohnAnderson even if I print(tokenList) right in the function it's still an empty list []. – q-compute Nov 17 '17 at 03:42
  • you have to call your function `main()` to run, you just defined it. – Tiny.D Nov 17 '17 at 03:43
  • @Tiny.D Well, I'm doing this in IDLE so you can either run it and type main() or add it to the end of the code. Which I actually did but did not paste. I'm not saying that I get no output. I'm saying that when I print it, I get [] instead of a full list. – q-compute Nov 17 '17 at 03:45

1 Answers1

4

You are trying to re-read file, you have to move the cursor to the beginning of the file:

file.seek(0)

change your code to:

import string

def main():
    fileInput = open("A tisket a tasket.txt",'r')    
    characters, words, lines = countEmUp(fileInput)
    fileInput.seek(0) #offset of 0
    printCounts(characters, words, lines)
    tokenList = splitToTokens(fileInput)
    print(tokenList)

def countEmUp(someFileInput):
    lines, words, chars = 0, 0, 0
    for line in someFileInput:
        chars += len(line)  # using += for nice and concise updates
        words += len(line.split())
        lines += 1
    return chars, words, lines

def printCounts(ch,wd,ln):
    print("Your file contains the following:")
    print("Characters:",ch)
    print("Words:",wd)
    print("Lines:",ln)

def splitToTokens(aFileInput):
    tokenList = []
    for line in aFileInput:
        tokenList.extend(line.split())
    return tokenList
main()
Tiny.D
  • 6,466
  • 2
  • 15
  • 20
  • This is spot on. This other post may help to understand it better https://stackoverflow.com/questions/3906137/why-cant-i-call-read-twice-on-an-open-file – ohjuny Nov 17 '17 at 04:00