Problems with my Python File IO code.

Started by
6 comments, last by Zahlman 19 years, 8 months ago
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?
The word vector means "carrier" in Latin; it is derived from the Latin verb vehere, which means to carry. (thefreedictionary.com)
Advertisement
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.
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 linesopenFile( f , 'test.txt' , 'w' )"""This way I don't have to do this"""f = openFile( 'test.txt' , 'w' )...
The word vector means "carrier" in Latin; it is derived from the Latin verb vehere, which means to carry. (thefreedictionary.com)
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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?
The word vector means "carrier" in Latin; it is derived from the Latin verb vehere, which means to carry. (thefreedictionary.com)
If you think about it, every non-primitive Python type *is* a pointer. So there you go.
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.
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 foobaz[0].append("spam")print baz # "[['spam'], [], []]"

This topic is closed to new replies.

Advertisement