def exportTextList(fileInput, wordList):
    fileOpen = open(fileInput)
    for line in fileOpen:
        line = line.rstrip("\n\r")
        lineList = line.split()
        wordList = wordList + lineList
    return wordList
        
def main():
    fileInput = 'PlayerNames.txt'
    wordList=[]
    exportTextList(fileInput, wordList)
    print wordList

My code summary:
I'm taking a txt file, playernames, and i'm stripping all of the line breaks and creating a list of each word in the file.
If i print wordList before I return it, I get the correct print out
But when I try to return it, and then print word list after in the main function, it just prints a blank list.

Anybody know what's going on?

Thanks in advance

Recommended Answers

Try this first:

def main():
    fileInput = 'PlayerNames.txt'
    wordList=[]
    print exportTextList(fileInput, wordList)   ## <-----

and then see the "gc_counter" function here.

Jump to Post

All 3 Replies

Try this first:

def main():
    fileInput = 'PlayerNames.txt'
    wordList=[]
    print exportTextList(fileInput, wordList)   ## <-----

and then see the "gc_counter" function here.

Yep woooee

That is it.
Spacemanspiff92 you heard him right.

The wordlist is blank cos the list wordlist got nothing to do with the wordlist in the exportT~ method.

;)

also if you are taking in list as methos arg.

its better you provide the arg as list.
like....

def foo(file_name, *word_list):
      #bla bla bla

This the wordlist is auto a list container.

;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.