2013-10-24 14 views
0

我的HTML代碼:如何HTML表單數據存儲到文件

<html> 
<head> 
<title>INFORMATION</title> 
</head> 
    <body> 
    <form action = "/cgi-bin/test.py" method = "post"> 
     FirstName: 
     <input type = "text" name = "firstname" /><br> 
     LastName: 
     <input type = "text" name = "lastname" /><br> 

     <input type = "submit" name = "submit "value = "SUBMIT"> 
     <input type = "reset" name = "reset" value = "RESET"> 
     </form> 
    </body> 

我的Python代碼(test.py),這是在cgi-bin目錄:

#!usr/bin/python 

form = web.input() 
print form.firstname 
print form.lastname 

什麼我應該如何將html數據存儲在某個文件中?

+0

重複http://stackoverflow.com/questions/13451400/saving-data-to-txt-file-using-python – shankar

回答

1

只要寫一個文件!

#!usr/bin/python 

import cgi 
form = cgi.FieldStorage() 
with open ('fileToWrite.txt','w') as fileOutput: 
    fileOutput.write(form.getValue('firstname')) 
    fileOutput.write(form.getValue'(lastname')) 

哦,你需要有寫入權限的文件。所以例如,如果你正在運行Apache,sudo chown www-data:www-data fileToWrite.txt應該這樣做。

+0

顯示:「test.py」 「test.py」E212:無法打開文件進行寫入 按ENTER鍵或鍵入命令繼續 –

+0

因此,您有一些其他進程打開文件,或者您沒有對該文件夾或文件的寫入權限 –

+0

順便說一句,這是由Linux機器上的'/ var/www'上的任何機會?如果是這樣,你需要根權限寫入文件或目錄 –

0
with open('/path/to/form.txt','w') as out_fh: 
    out_fh.write(form.firstname) 
    out_fh.write(form.lastname 

的Web服務器需要有寫權限要在創建該文件的目錄。

+0

每當我保存它顯示:「test.py」「test.py」E212:無法打開文件進行寫入按ENTER鍵或輸入命令,以便每當我保存時繼續 –

相關問題