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.

#!/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 ?
Edit/Delete Message

Recommended Answers

All 3 Replies

I ran a quick test and on my Windows XP Box it seems to be the file path it balks at. I had to give it the full file path (or at least back to where python.exe sits)

try:
    # needs full file path!
    OutFile = open("/home/nephish/Test.txt", "w")
    OutFile.write("Here is a spot of text to input")
    OutFile.close()
    print "File successfully written"
except IOError:
    print "Cannot open file for writing"

OK, Here is what i got.
i got the correct text back when i submitted the form
then it printed out
"Cannot open file for writing"
i wonder if its a permissions issue, but i am running this from the user
'nephish' so perhaps its something else.

thanks, by the way.

OH Hey, it was a permission problem.
i made a directory off root called /files and did a chmod 777 to it
rewrote the path to /files and ran it again and it worked this time.
thanks!

back in action....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.