How would you open this file?

B

Bob

I want to open a file in a function and pass the file back to main so
another function can manipulate the file, something like this:

# begin
def open_file()
filename=open(options.filename_input,'r')
return filename

open_file()
print filename.read()
filename.close()
# end

But this doesn't work... It appears that "open_file()" needs a
variable, but in order to pass a variable to "open_file()" I first need
a variable... but I would rather have the function send me "filename"
without creating it main. Is that possible?

Is there a better way to have a function open a file and pass the file
back to main than what I am trying to do? Thanks!
 
K

Kent Johnson

Bob said:
I want to open a file in a function and pass the file back to main so
another function can manipulate the file, something like this:

# begin
def open_file()
filename=open(options.filename_input,'r')
return filename

open_file()
print filename.read()
filename.close()
# end

You need to assign the result of open_file() to a variable. The
'filename' variable inside open_file() is not available outside the
function:

filename = open_file()

By the way 'filename' is a pretty bad name, since it contains a file
object, not a string. Maybe call it f instead. ('file' is also a bad
name because it is the name of a Python built-in function.)

Kent
 
D

Dave Hansen

On Thu, 23 Feb 2006 18:01:32 -0500 in comp.lang.python, Kent Johnson

[...]
filename = open_file()

By the way 'filename' is a pretty bad name, since it contains a file
object, not a string. Maybe call it f instead. ('file' is also a bad
name because it is the name of a Python built-in function.)

I write a lot of simple scripts. Those that have input and/or output
files tend to call them infile and outfile. Given the name of the
OP's file, perhaps optfile would work for him...

Regards,
-=Dave
 
L

Larry Bates

Bob said:
I want to open a file in a function and pass the file back to main so
another function can manipulate the file, something like this:

# begin
def open_file()
filename=open(options.filename_input,'r')
return filename

open_file()
print filename.read()
filename.close()
# end

But this doesn't work... It appears that "open_file()" needs a
variable, but in order to pass a variable to "open_file()" I first need
a variable... but I would rather have the function send me "filename"
without creating it main. Is that possible?

Is there a better way to have a function open a file and pass the file
back to main than what I am trying to do? Thanks!

Your function returned filename but in the main program you didn't
have anywhere for it to be saved.

fp=open_file()
print fp.read()
fp.close()

Others have pointed out the filename is a bad choice for a
variable name as it is a pointer to a file object. Most
tutorials and a lot of standard library code use fp.

-Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top