954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

simple cgi script issue

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

ruwach
Newbie Poster
8 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

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"
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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.

ruwach
Newbie Poster
8 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

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....

ruwach
Newbie Poster
8 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You