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