CMPT165-Files.docx

26 views3 pages
Page:
of 3
Files
•Files are used to store data when a program or script is not running
•When data is stored in a file, the data will remain there until we need it again
•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
Writing
•We start with writing because writing always comes before reading
-That is, we have to put data into a file before we can take it out
•You need one form/script that gets data (from the user) and writes it to a file
•Then you need a second form/script that retrieves the data (reads the file) and displays it to
the user
•Here is a script, personalInfo.py
-You must download file.pyc to make this script work
import cgi
form = cgi.FieldStorage()
from file import * # <-- This is new!
print "Content-type: text/html"
print
print "<html>"
print "<head>"
print "<title></title>"
print "</head>"
print "<body>"
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>"
from file import *
-This line allows you to work with files
-For this to work, you must download file.pyc from the course website, and put it in the same
folder as your Python script
f = File("personalInfo.data")
-This says that the variable f refers to the file personalInfo.data
f.startWrite()
-This line prepares the file that f refers to (personalInfo.data in this example) for writing
-This line will do different things under different circumstances
-If the file that f refers to does not yet exist, then it will be created
-If the file that f refers to already exists, then then everything in it will be deleted
f.write(firstName)
-This line writes the value of the variable firstName into the file that f refers to
(personalInfo.data in this example)
f.close()
-This line indicates that you're done with the file that f refers to
-It ensures that everything is correctly written
-It also allows other programs to use the file that f refers to
Reading
•Now that data has been saved in a file, we'll need another form/script to retrieve it
•The script, personalInfoRetrieve.py
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>"
f = File("personalInfo.data")
f.startRead()
firstName = f.read()
f.close()
print "<p>Your first name is " + \
firstName + ".</p>"
print "</body>"
print "</html>"
f = File("personalInfo.data")
-f refers to the file personalInfo.data
f.startRead()
-The line prepares the file that f refers to for reading
firstName = f.read()
-The line reads the first piece of data in the file that f refers to, and puts it into the variable
firstName
f.close()
-This line indicates that you're done with the file that f refers to
•Sometimes, there can be problems when reading files. What if we try reading data from a file
that doesn't exist?
-You can't take data out of a file that doesn't exist
•Why would a file not exist?
-It might not have been created
-Someone could have deleted/moved/ renamed it
•Trying to read from a file that doesn't exist will cause an error, and your script will crash
•So, before reading any file, you must first check to see that it exists
f = File("personalInfo.data")
if f.exists() == True: # <-- this is new
f.startRead()
firstName = f.read()
f.close()
print "<p>Your first name is " + \
firstName + ".</p>"
else:
print "<p>You don't exist.</p>"
•The exists() function checks to see if a file exists or not
-It will give a value of True or False