{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CMPT165_12_Files - Files Saving stuff for later Colin...

Info icon This 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: headright the user types in some data (name, age, etc.) headright 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
Image of page 1

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

View Full Document Right 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 headright when the script finishes, the data stored in the variables disappears
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 … headright reading is called opening headright writing is called saving
Image of page 3

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

View Full Document Right Arrow Icon
–4 Writing Writing let's look at an example of writing data writing always comes before reading headright 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>
Image of page 4
–5 Writing Writing the form/script, in action: barb4right here, the script saves the first name in a file Writing Writing the script, personalInfo.py : headright 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
Image of page 5

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

View Full Document Right 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 *
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern