Often times programs uses files to get initial input values and to write out results before exiting. This way starting up a program can be fast (the user doesn't have to enter all the input by hand), and the effects of a program can persist (we can keep around the results of a program run in a file. One program can be used to create as its output an input file for another program (further easing the user's having to create a huge input file).
One example of a type of program that use files data is a database program. A company keeps information about Employees in a file that are read in by a database program when employee information needs to be accessed or updated. The database program will read in the database file on start-up and write any modifications to the database file before it exits. For example, if an employee gets a raise, the database program reads in the employee file, finds the employee's information from the file, updates it, and writes the resulting change in the employee's salary to the employee file so that it is not lost when the database program exits.
Here are some example calls to open:
# open foo.txt for reading (the file's relative path name is used in this example) infile = open("foo.txt", "r") # open blah.txt for writing (the file's absolute path name is used in this example) outfile = open("/home/newhall/blah.txt", "w")If the file is successfully opened, open returns a file object associated with the open file.
To read or write to a file, you need to call methods of the file object returned by the call to open. To list file documentation, in the python interpreter:
$ python >>> help(file)
Here are a few read methods that may be useful:
ch = infile.read(1) # read one character from the file # returns: a single char string # or "" if there are no more chars to read # curr position: advanced to next char in file line = infile.readline() # read all the characters from the current pos # to the end of the current line (up to and # including the eoln character '\n') # returns: a string containing the next line # or "" if there are no more lines to read # curr position: first char on the next line lines = infile.readlines() # read in every line of the file up to the end # returns: a list of strings, one string # per line in the file (each string # includes the eoln char at the end) # curr position: at the end of the fileFor example, if the file foo.txt contains:
hello there hiThen:
infile = open("foo.txt", "r") lines = infile.readlines() infile.close()Reads in the entire contents of the file; readlines returns a list of three strings, one for each line in the file. The variable lines is a list containing (notice the \n char at the end of each string):
[ "hello\n", "there\n", "hi\n" ]
infile.close()