# exp7.py example file read and write files of various types def main(): line1 = "line1\n" # needs \n new line line2 = "line2\n" line3 = "line3\n" print "exp7.py running" print "write a file exp7file.txt" file7 = open("exp7file.txt","w") # open a file for writing file7.write(line1) # creates file, overwrites if exists file7.write(line2) file7.write(line3) file7.close() # close the file print "file written" print "read the entire file just written" inp = open("exp7file.txt","r") # open existing file for reading inputs = inp.read() # reads entire file inp.close() # close the file print "read from exp7file.txt file" print inputs print "read a line at a time" inp = open("exp7file.txt","r") # open existing file for reading input1 = inp.readline() # reads one line print input1 input1 = inp.readline() # reads next line print input1 input1 = inp.readline() # reads next line print input1 inp.close() # close the file # read numbers from file print "read numbers from file data.txt" with open('data.txt') as f: polyShape = [] for line in f: line = line.split() # to deal with blank if line: # lines (ie skip them) line = [int(i) for i in line] polyShape.append(line) print polyShape if __name__ == '__main__': main()