Sign in to follow this  

Problems with my Python File IO code.

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is the complete module 'FileIO.py':
"""
fileIO.py
"""

def openFile( fileName , fileMode ) :
    """
    Opens a file for you.
    """
    f = file( fileName , fileMode )
    return f

def closeFile( fileObject ) :
    """
    Closes a file for you.
    """
    if fileObject.closed :
        print "ERROR: FILE ALREADY CLOSED!"
        return
    fileObject.close( )
    return

def writeFile( fileObject , text ) :
    """
    Writes to a file for you.
    """
    if fileObject.mode == 'r' :
        print "ERROR: CANNOT WRITE TO FILE! IN MODE \"R\".\n"
        return
    elif fileObject.mode == 'rb' :
        print "ERROR: CONNOT WRITE TO FILE! IN MODE \"RB\".\n"
        return
    fileObject.write( text )
    return

def readFile( fileObject ) :
    """
    Reads the entire file for you.
    """
    if fileObject.closed :
        print "ERROR: FILE ALREADY CLOSED!\n"
        return
    print fileObject.read( )
    return

def run( ) :
    f = file( 'test.txt' , 'w' )
    writeFile( f , "Hello world." )
    readFile( f )
    closeFile( f )
    raw_input( )
    return

if __name__ == '__main__' :
    run( )



And here is where I suspect the problem might be:
...
def readFile( fileObject ) :
    """
    Reads the entire file for you.
    """
    if fileObject.closed :
        print "ERROR: FILE ALREADY CLOSED!\n"
        return
    print fileObject.read( )
    return
...



Here is the output:
Xõ;Ì

Sometimes it even puts the entire source code in the file! Am I having scope problems here? And does Python use memory addresses or copies when passing actual arguments to functions?

Share this post


Link to post
Share on other sites
The file is still in mode 'w' when you attempt to read from it. You can close it and open again in mode 'r' before reading. Or if you start by opening in mode 'r+' (read and write) you should be able to leave the rest of the code as is.

Share this post


Link to post
Share on other sites
The 'r+' didn't work. However, closing it then reopening it for reading did. Thank you.

Also, does any one know how to assign a variable a null file object? For instance:


...
f = NULL # or along these lines
openFile( f , 'test.txt' , 'w' )
"""
This way I don't have to do this
"""
f = openFile( 'test.txt' , 'w' )
...


Share this post


Link to post
Share on other sites
f = None

But that's still not going to do what you want, since an assignment to f within your function (like f = open(fileName, fileMode )) will reassign the local reference without affecting the outer f.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
But that's still not going to do what you want, since an assignment to f within your function (like f = open(fileName, fileMode )) will reassign the local reference without affecting the outer f.


Thank you.

At this point, pointers come in to play with C/C++. Is there an equivalent "pointer" within the Python language?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
If you think about it, every non-primitive Python type *is* a pointer. So there you go.


Not really. Every variable is a reference, and they really do not have types. The objects variables reference have types, of course.

Share this post


Link to post
Share on other sites
Quote:
Original post by V E C T O R
Quote:
Original post by Fruny
But that's still not going to do what you want, since an assignment to f within your function (like f = open(fileName, fileMode )) will reassign the local reference without affecting the outer f.


Thank you.

At this point, pointers come in to play with C/C++. Is there an equivalent "pointer" within the Python language?


Python's object model is basically like Java's, except without primitives (everything is an object).

There are certainly ways to achieve an extra level of indirection. You can wrap it in any container type, for example. However, you usually don't need to do anything along those lines, because you generally *don't* have copies of objects unless you ask for them:


foo = []
bar = [foo] * 3 # every element of bar is the *same* []
bar[0].append("spam")
print bar # "[['spam'], ['spam'], ['spam']]"
print foo # "['spam']"
foo = []
baz = [copy(foo), copy(foo), copy(foo)] # every element of baz is a *different* *copy* of the [] in foo
baz[0].append("spam")
print baz # "[['spam'], [], []]"

Share this post


Link to post
Share on other sites

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this