#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    10
    Rep Power
    0

    simple cgi script snafu


    Hey there, i need some help with a simple cgi script.
    i cannot get it to write output to a file.
    here is the script.
    Code:
    #!/usr/bin/python
    
    import cgi
    
    form = cgi.FieldStorage()
    Customer = form['Customer'].value
    Field = form['Field'].value
    
    data = [Customer, Field]
    
    
    reshtml = """Content-Type: text/html\n
    <html>
     <head><title>Customer Data</title></head>
    
     <body>
      <h1>Customer Data</h1>
      <p>Here ya go</p>
     </body>
    </html>"""
    
    
    print reshtml + 'Customer =' + data[0]
    print '    ' + '    Field = ' + data[1]
    
    # this is where the script fails to do anything....
    OutFile = open("/home/nephish/Test.txt", "w")
    OutFile.write("Here is a spot of text to input")
    OutFile.close
    it works, in that it returns the correct info to the browser,
    but it does not write a text file.
    i need to be able with this script to store on my disk what is input
    in the html form.

    any suggestions ?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    well as I think you need a full path for teh file.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    The Holographic Universe
    Posts
    75
    Rep Power
    10
    You haven't closed the file, so it is still open when the script ends and thus isn't saved. You need to call the close method of the file object instead of reference it.

    Code:
    OutFile.close()
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    10
    Rep Power
    0
    ok, changed
    outfile.close to
    outfile.close() and tried to rerun with same results.
    i put the file writing part on the other side of the part where it
    displays results to the browser (so it would do that first) and i got an
    internal server error.
    eek- this seems like it should be so simple.
    looks like this now:


    Code:
    reshtml = """Content-Type: text/html\n
    <html>
     <head><title>Customer Data</title></head>
    
     <body>
      <h1>Customer Data</h1>
      <p>Here ya go</p>
     </body>
    </html>"""
    
    
    OutFile = open("/home/nephish/Test.txt", "w")
    OutFile.write("Here is a spot of text to input")
    OutFile.close()
    
    print reshtml + 'Customer =' + data[0]
    print '   ' + '     Crop = ' + data[2] 
    print '    ' + '    Field = ' + data[1]

    thanks for your help, any suggestions?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    WHOOPS! Nothing to see here.
    Last edited by Shiner_Man; May 27th, 2005 at 10:45 AM.
    I'll learn this stuff someday.

IMN logo majestic logo threadwatch logo seochat tools logo