tags:

views:

122

answers:

2

I am trying to written this code which would just test whether a file exists and then reads it and prints. I have written this code as a file named readFile.py and trying to run it through shell using execfile command. I have put many print stmts to check till where the control is going. The result shows me only first two print stmts and the control is not entering the def readFile()..I am unable to find the reason and need help.Thanks!!

print 'i am doing fine'
filename = "train-win.dat"
print 'i am doing fine1'
def readFile():
        print 'i am doing fine2'
        import os
        print 'i am doing fine3'

        if os.path.exists(filename):
                print 'i am doing fine4'
                f = open(filename,"r")
                print 'i am doing fine5'
                a = f.readlines()
                print 'i am doing fine6'
                print a
                print 'i am doing fine7'
        f.close()p
+7  A: 

You define the function readFilebut you haven't called it so it will never execute. Add this at the end of your file (not indented):

readFile()

Also you have a syntax error on the last line of the function:

 f.close()p

That p shouldn't be there.

After making both these changes your program seems to work.

Mark Byers
+5  A: 

While your code will work with minor modifications shown in another answer, the usual way of writing Python presents code in a slightly different order. Without the extra print statements, I might write:

import os

def readFile():
    if os.path.exists(filename):
        f = open(filename, "r")
        a = f.readlines()
        print a
        f.close()

filename = "train-win.dat"
readFile()

The first thing is to import the modules. Normally this is done at the top of the file.

The next part defines a function called readFile. The def statement doesn't actually do anything at the time it's executed, but Python remembers the statements within the block to be executed later.

Finally, readFile() actually calls the readFile function.

Note also that I moved f.close() inside the if statement. You wouldn't want to try to close f if it had never been opened in the first place.

Greg Hewgill
+1 Good points.
Mark Byers
I'm being pedantic but the `def` statement _does_ actually do something: It creates an entry in `f_locals` attribute of the current stack frame keyed by the name of the function and sets up any closures required by the function (among other things). It is a full, first-class statement: the program is in a different state before and after it is executed as opposed to, say, C.
aaronasterling
@aaronsterling: While you're right, I don't think that level of detail is useful for somebody who's clearly learning Python for the first time.
Greg Hewgill
You lost the `open()` call...
Zack
@Zack: good eyes, thanks!
Greg Hewgill
Saying that it doesn't do anything is misleading though. It creates a function value and assigns it to the functions name. If that's hard for somebody to understand then they need to suck it up and understand it anyways because it's important.
aaronasterling
`for line in open(filename, "r"): print line` is arguably even better, stylistically, but also maybe a bit too telegraphic for someone just learning the language.
Zack
@aaronsterling: I did say, "Python remembers the statements within the block to be executed later." I just didn't refer to technical details like `f_locals` or "stack frame" or "closures" which I guarantee won't mean anything to a beginning programmer.
Greg Hewgill