How would you open this file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob

    #1

    How would you open this file?

    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(o ptions.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!

  • Kent Johnson

    #2
    Re: How would you open this file?

    Bob wrote:[color=blue]
    > 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(o ptions.filename _input,'r')
    > return filename
    >
    > open_file()
    > print filename.read()
    > filename.close( )
    > # end[/color]

    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

    Comment

    • Dave Hansen

      #3
      Re: How would you open this file?

      On Thu, 23 Feb 2006 18:01:32 -0500 in comp.lang.pytho n, Kent Johnson
      <kent@kentsjohn son.com> wrote:

      [...][color=blue]
      >
      >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.)[/color]

      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.

      Comment

      • Larry Bates

        #4
        Re: How would you open this file?

        Bob wrote:[color=blue]
        > 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(o ptions.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!
        >[/color]

        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

        Comment

        • Bob

          #5
          Re: How would you open this file?

          Thanks all! That helps out a lot!

          Python is kinda' cool...

          Comment

          Working...