CMPT165_12_Files

CMPT165_12_Files - Files Saving stuff for later Colin...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
–1 Files Saving stuff for later © Colin Stewart, 2009 Writing data Writing data • all the forms so far that work like this: h the user types in some data (name, age, etc.) h our script then makes an HTML page that tells them what they just typed in • the data that they type in is not actually writed anywhere – it's just displayed in an HTML page • at a later time, if we wanted to retrieve the data that we'd typed in, we couldn't do it • these kinds of forms/scripts are pretty useless
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
–2 Writing data Writing data • a much more useful form/script would write the data that was typed in, and would allow us to retrieve the data any time in the future • if we want to do this, we will have to store the user's data somewhere Writing data Writing data • currently, the only method we have for storing data is to use variables in a script • but variables only store data while the script is running h when the script finishes, the data stored in the variables disappears
Background image of page 2
–3 Files Files • solution: files • files are used to store data when a program/ script is not running • when data is stored in a file, the data will remain there until we need it again Files Files • there are two main actions involved with files: reading and writing • reading a file means taking data out of a file • writing means putting data into a file • in most programs that you're familiar with … h reading is called opening h writing is called saving
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
–4 Writing Writing • let's look at an example of writing data • writing always comes before reading h that is, we have to put data into a file before we can take it out • we'll need one form/script that gets data (from the user) and writes it to a file • then we'll need a second form/script that retrieves the data (reads the file) and displays it to the user Writing Writing • we'll start with a simple example: an HTML form that asks for a person's name: <form action=" personalInfo.py " > <p> First name: <input type= "text" name= "firstName" /></p> <p><input type= "submit" value= "Save data" /> </p> </form>
Background image of page 4
–5 Writing Writing • the form/script, in action: b • here, the script saves the first name in a file Writing Writing • the script, personalInfo.py : h you must download file.pyc from the course website to make this work import cgi form = cgi.FieldStorage() from file import * print "Content-type: text/html" print print "<html>" print "<head>" print "<title></title>" print "</head>" print "<body>" #continued on next slide
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
–6 Writing Writing #continued from previous slide firstName = form[ "firstName" ].value f = File("personalInfo.data") f.startWrite() f.write(firstName) f.close() print "<p>First name saved successfully!</p>" print "</body>" print "</html>" Writing Writing • things to note: from file import * h this allows you to work with files
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/30/2009 for the course CMPT 165 taught by Professor Ramesh during the Summer '09 term at Simon Fraser.

Page1 / 22

CMPT165_12_Files - Files Saving stuff for later Colin...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online