-1

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
3
  • You have to reduce indentation when removing the def main(): line. So, unindent the following two to get if __name__ ... aligned with column #0 Commented Feb 17, 2011 at 23:52
  • @user612041 - You have indent your program properly. if should start at col 1 and not within def populateArray block. Commented Feb 17, 2011 at 23:52
  • Thank you for all your help - I am getting output now, well two lots, though I don't mind - the fun will start when parsing my file data to a 2D list i'm sure! Commented Feb 18, 2011 at 0:40

5 Answers 5

5

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.

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

2 Comments

There is no \t in the string -- non need for a raw string here.
\t would be converted to tab, but /t would not. In this context I don't think putting r in front of the string would change anything.
1

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.

Comments

0

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)

Comments

0
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!

Comments

-1

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..

1 Comment

You need that since otherways he's manipulating just the network inside populateArray() that gets lost when the function execution terminates, not the one defined in the global scope.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.