0
Test.py代碼將HTML和打印到文件的值獲取跟蹤錯誤 爲什麼文件描述符的錯誤正在進行。我是一個新手。TypeError:不支持的操作數類型爲+:'NoneType'和'str'
#!/usr/bin/python
import cgi
def get_data():
'''
This function writes the HTML data into the file
'''
print "Content- type : text/html\n"
form = cgi.FieldStorage()
f = open("abc.txt","w")
f.write(form.getvalue('firstname'))
f.write(form.getvalue('lastname'))
f.write(form.getvalue('age'))
f.write(form.getvalue('gender'))
f.close()
#print "Hello ", Fname, Lname, Age, Gender
get_data()
Tracebacjk錯誤:
Traceback (most recent call last):
File "test.py", line 33, in <module>
get_data()
File "test.py", line 25, in get_data
f.write(form.getvalue('firstname') + '\n')
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
我的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>
Age:
<input type = "text" name = "age" /><br>
Gender:
<input type="radio" name="gender" value="male" /> Male
<input type="radio" name="gender" value="female" /> Female
<input type = "submit" name = "submit "value = "SUBMIT">
<input type = "reset" name = "reset" value = "RESET">
</form>
</body>
添加的HTML file.PLease檢查
當使用第一個選項錯誤:回溯(最近通話最後一個): 文件 「test.py」,33行,在 GET_DATA() 文件 「test.py」,第25行,在get_data Type.Error:不支持的操作數類型爲+:'NoneType'和'str' –
with是Python 2.6中的保留關鍵字,所以我我無法使用這個 –
@johnjohn,我更新了答案來處理POST參數丟失時的情況。一探究竟。 – falsetru