Public Group

# Problems with my Python File IO code.

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

## 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 :
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

"""
Reads the entire file for you.
"""
if fileObject.closed :
return
return

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

if __name__ == '__main__' :
run( )


And here is where I suspect the problem might be:
...
"""
Reads the entire file for you.
"""
if fileObject.closed :
return
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 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 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 linesopenFile( f , 'test.txt' , 'w' )"""This way I don't have to do this"""f = openFile( 'test.txt' , 'w' )...

##### 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 on other sites
Quote:
 Original post by FrunyBut 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 on other sites
If you think about it, every non-primitive Python type *is* a pointer. So there you go.

##### Share on other sites
Quote:
 Original post by Anonymous PosterIf 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 on other sites
Quote:
Original post by V E C T O R
Quote:
 Original post by FrunyBut 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'], [], []]"

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 19
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633362
• Total Posts
3011531
• ### Who's Online (See full list)

There are no registered users currently online

×