Python Source Code: fwrite.py
#!/usr/bin/env python
import cgi
import textwrap
import os
import common
#----------------------------------------------------------
# The Web Language Project
# Mark Brautigam
# May-June 2015
# http://www.mixed-up.com/markb/
#----------------------------------------------------------
common.headers1();
common.headers2();
print common.sidebarX("python", "fwrite", "py");
print textwrap.dedent("""\
<div id="content">
<h2>Python: Form Handling and Write to File</h2>
""")
#-------------------------------------------------------------
def printForm () :
#-------------------------------------------------------------
print textwrap.dedent("""\
<form name='coursesform' action='fwrite.py' method='POST'>
<fieldset>
<legend>Add a course</legend>
<table>
<tr><td>College:</td><td><input type='text' name='college' /></td></tr>
<tr><td>Department:</td><td><input type='text' name='dept' /></td></tr>
<tr><td>Course #:</td><td><input type='text' name='course' /></td></tr>
<tr><td>Course name:</td><td><input type='text' name='desc' /></td></tr>
<tr><td></td><td><input type='submit' id='submit' name='submit' value='Add' /></td></tr>
</table>
</fieldset>
</form>
""")
#-------------------------------------------------------------
def formHasData (formData) :
#-------------------------------------------------------------
nData = len(formData)
for i in range (0, nData) :
if (formData[i].strip() != '') :
return True;
return False;
#-------------------------------------------------------------
def printFeedback (college, dept, course, desc) :
#-------------------------------------------------------------
print "<p>The following data was entered in the data file: </p>";
print "<table class='results'>\n";
#print " <tr><td>College</td><td>{}</td></tr>".format(college); #Python 2.6 only
#print " <tr><td>Department</td><td>{}</td></tr>".format(dept); #Python 2.6 only
#print " <tr><td>Course #</td><td>{}</td></tr>".format(course); #Python 2.6 only
#print " <tr><td>Course name</td><td>{}</td></tr>".format(desc); #Python 2.6 only
print " <tr><td>College</td><td>%s</td></tr>" % (college);
print " <tr><td>Department</td><td>%s</td></tr>" % (dept);
print " <tr><td>Course #</td><td>%s</td></tr>" % (course);
print " <tr><td>Course name</td><td>%s</td></tr>" % (desc);
print "</table>"
#-------------------------------------------------------------
# The main Python code is here.
#-------------------------------------------------------------
printForm();
form = cgi.FieldStorage()
college = form.getvalue('college', '')
dept = form.getvalue('dept', '')
course = form.getvalue('course', '')
desc = form.getvalue('desc', '')
submit = form.getvalue('submit', '')
if (submit == 'Add' and formHasData([college, dept, course, desc])) :
printFeedback (college, dept, course, desc);
# Beware. The print function above includes CR by default.
# BUT the fp.write function below does not include the CR.
fp = open ('../data/courses.txt', 'a')
#dataToWrite = "{}|{}|{}|{}\n".format(college,dept,course,desc) #Python 2.6 only
dataToWrite = "%s|%s|%s|%s\n" % (college,dept,course,desc)
print (dataToWrite)
#fp.write ("{}".format(dataToWrite)) #Python 2.6 only
fp.write ("%s" % (dataToWrite))
fp.close
#-------------------------------------------------------------
print textwrap.dedent("""\
<p><a href='fread.py'>Show the contents of this file »</a></p>
<p><a href='source.py?f=3'>Show Python source code »</a>
</div>
""")
common.footers();