""" Produced by Andrew Jason Penner Sept 31 2007 """ #! /usr/bin/python """ We may want to try formatted output """ s = 'Hello World' print 'This is the string s',str(s) print 'This is another way of producing the string s, note the quotes around the string.',repr(s) """ repr() returns the litteral, all special characters etc... """ """ This is one way to display a table. rjust(n) justifies the current column to a width of n. """ for x in range(1,11): print repr(x).rjust(2), repr(x**2).rjust(3), print repr(x**3).rjust(4) """ Here we take on a C style output formatting """ for x in range(1,11): print '%2d %3d %4d' %(x,x**2,x**3) """ zfill(n) looks for n characters in the previous string, failing to find all n, it pads the rest with zeros. If there are more than n characters it does nothing. """ print 'This is the zfill command','12'.zfill(5) import math print 'The value of PI is approximately %5.3f.' % math.pi """ This concludes the section on formating the output, as before there are several other commands but I do not see the need to drone on about it """ """ Now we concern ourselves with reading and writing to files """ """ We see below that the method of opening files is just as we would see in C The first argument is the filename, the second is the mode, w for writing which will clobber an existing file of the same name, r for reading, r+ for reading and writing, a for appending to an existing file. If the mode is omitted, r is assumed. On windows and Macs, append b to any of the above for binary files. Linux makes no decision between ascii and binary files. Below f is referred to as a file pointer. """ f=open('/tmp/workfile','w') """ a module read(size) appended to an open file will read a quantity of data and returns it as a string. If one omits 'size' the entire file is read. If the end of file is reached it will return an empty string. readline reads the lines of the file, one line at a time readlines reads all the complete lines in a file, returning them as a list """ #f.read() #f.readline() #f.readlines() """ the module write(string) writes the contents of string to a file. to write things that are not strings you must convert them to strings first, using str(). """ #f.write('hello world') """ Further modules of interest: tell() returns an integer giving an object's current position in bytes seek(offset,from_object) This function adds offset to the position of from_object. If from_object is 0 it seeks from the beginning of file. If from_object is 1 it uses the current file position (not always a safe option), if from_object is 2 it seeks from the end of file. If from_object is omitted, it defaults to 0. """ #f.tell() #f.seek(5) """ Finally when one is done with a file you close it using close(). No arguments are necessary. """ #f.close() """ The last module to be discussed here is pickle pickling is the process of converting a nonstring element into a string element (pickle.dump(nonstring,file_pointer)) to reverse this one is unpickling (pickle.load(file_pointer)) """ #pickle.dump(x,f) #pickle.load(f)