user612041
user612041

Reputation: 215

Python Function Argument

I am having two issues with my code - I am not sure how to link my populateArray function with my main function; I'm not sure what argument I need to pass Also, I have been having trouble with the file path of the file to be opened - the path is correct and the file exists with data in. Here is my code:

network = []  

def populateArray():
    file = open('theroute.txt', 'r')
    network = []  

    for line in file:

        network.append(line)

    print "Network = "
    print network

    file.close()

def main():    
    if __name__ == "__main__":
       populateArray()

Any help would be appreciated!

Thanks for your replies - my code now looks like the above, but when I remove def main(): I get the following error:

File "populateArray.py", line 18
    if __name__ == "__main__":
                             ^
IndentationError: unindent does not match any outer indentation level

Upvotes: -1

Views: 384

Answers (5)

RichMeister
RichMeister

Reputation: 124

Remove line 5 (the extra "network=[]") and the def main() which you don't need. And pass network as an argument to the populateArray function and it will work.

network = []  

def populateArray(network):
    file = open('theroute.txt', 'r')

    for line in file:
        network.append(line)    
    file.close()


if __name__ == "__main__":
        print "Start"
        populateArray(network)
        print "Network = "
        print network

For the file path: with no path, you are pointing at the Python home directory (e.g. C:\Python27 for Python 2.7)

If you use / that points to the root directory. To point to your home directory point to the directory (e.g /home/myuserid/theroute.txt or /Users/myuserid/theroute.txt)

Upvotes: 0

Hugh Bothwell
Hugh Bothwell

Reputation: 56644

def parseLine(line):
    # do something to input line!
    return line.strip().split()

def populateArray(fname, fn=None):
    with open(fname, 'r') as inf:
        if fn:
            return map(fn, inf)
        else:
            return inf.readlines()

def main():
    network = populateArray('/usr/jim/data/theroute.txt', parseLine)

if __name__ == "__main__":
    main()
  • globals are almost always a sign of poor design

  • parameterize your functions so you can reuse them later

Hope this helps!

Upvotes: 0

Senthil Kumaran
Senthil Kumaran

Reputation: 56841

Remove def main():, just have it as:

if __name__ == "__main__":
   populateArray()

Make sure to indent your program properly after you remove the def stmt.

You can also refer to your file name directly, if you are in the same directory.

Upvotes: 5

redShadow
redShadow

Reputation: 6777

If you want to populate, as I guess, the "network" list (not "array"!) in the global scope, you need to bring it global.

def populateArray():
    global network
    # The rest of the function code goes here

But, i'd rather do something like:

network = populateArray()

and make populateArray() return the network list.

Or you can pass network as an argument to the function and manipulate it (most objects are passed by reference in Python). Remember you cannot do network = [] inside the function, or the reference will be lost and replaced with a brand new object. Use del network[:] instead to empty the list.

Then, of course, you have to run something outside a function to be executed when running the script, so remove the def main() to get only:

if __name__ == "__main__":
   populateArray()

or, if you want to use a main() function:

def main():
    populateArray()

if __name__ == "__main__":
   main()

(although it is pretty useless in this example..)

UPDATE: I just noticed you are calling your file object file.. do not do that since file is the name of a Python built-in object (the one returned by open() actually), and it wouldn't be a very good idea to monkey-patch that..

Upvotes: -1

swiecki
swiecki

Reputation: 3483

The path of the file to be opened is relative to the where your .py file is. For example, if they are in the same folder, then you can simply do a

file = open('theroute.txt', 'r')

Hope this helps.

Upvotes: 1

Related Questions