473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How would you open this file?

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!

Feb 23 '06 #1
4 1241
Bob wrote:
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
Feb 23 '06 #2
On Thu, 23 Feb 2006 18:01:32 -0500 in comp.lang.python, Kent Johnson
<ke**@kentsjohnson.com> wrote:

[...]

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

--
Change is inevitable, progress is not.
Feb 24 '06 #3
Bob wrote:
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
Feb 24 '06 #4
Bob
Thanks all! That helps out a lot!

Python is kinda' cool...

Feb 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: yanivmad | last post by:
hi I like to ask if there any option to control the html files?!? I have some HTML files at my web site that use one "Help.htm" file for all the pages {I use the "window.open(.... " option at...
9
by: Charles F McDevitt | last post by:
I'm trying to upgrade some old code that used old iostreams. At one place in the code, I have a path/filename in a wchar_t string (unicode utf-16). I need to open an ifstream to that file. ...
6
by: Dino Buljubasic | last post by:
My application creates some temporary files that are deleted when my application terminates. However, if a temp file is open, it will not be deleted and application will crash. How can I...
2
by: Mattbooty | last post by:
Hello, Not sure if anyone else has seen this bug, but I have a form where the entire form is covered with a picturebox. The picturebox has a mouseup event. I also have an open file dialog for...
6
by: qysbc | last post by:
I have a web page and there is a link to open a TIFF file. The way I do it is to have the server code open a binary stream, set the content type to "image/tiff" and call Response.BinaryWrite. On...
2
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question in relation to opening files from C#. I have a Windows form where I allow the user to type in a file extension in a text box (e.g., "xls"). I then take...
6
by: Moumen VB.NET 2003/2005 Developer | last post by:
How can I detect if a file sitting on a network drive is still open by another application? This application resides on another machine on the network? I am using VB.NET 2003 Your help is...
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
6
by: Ros | last post by:
There are 10 files in the folder. I wish to process all the files one by one. But if the files are open or some processing is going on them then I do not want to disturb that process. In that case...
0
by: Ofelia | last post by:
Hi, I'm new to this forum and to Perl language but I would like to ask for your help. I'm working in Linux and the files I need to process are in the format “file.gz”. I created a script which...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.