Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] Type "help", "copyright", "credits" or "license" for more information. >>> # we've seen files a couple places >>> # e.g. pic = media.load_picture(filename) >>> # You have to open a file first before reading from it >>> myfile = open('/Users/janwiebe/Documents/Courses/CS0007/fairytale.txt','r') >>> type(myfile) >>> # To get the contents, you need to read from the file >>> filename = '/Users/janwiebe/Documents/Courses/CS0007/fairytale.txt' >>> filename '/Users/janwiebe/Documents/Courses/CS0007/fairytale.txt' >>> # 'r' is for read; 'w' is for destructive write; 'a' is to add to the file >>> # the default is 'r' >>> myfile >>> >>> # read everything in a file >>> x = myfile.read() >>> type(x) >>> x "There was once a man whose wife died, and a woman whose husband\ndied, and the man had a daughter, and the woman also had a\ndaughter. The girls were acquainted with each other, and went\nout walking together, and afterwards came to the woman in her\nhouse. Then said she to the man's daughter, listen, tell your\nfather that I would like to marry him, and then you shall\nwash yourself in milk every morning, and drink wine, but my own\ndaughter shall wash herself in water and drink water. The girl\nwent home, and told her father what the woman had said. The\nman said, what shall I do. Marriage is a joy and also a torment.\nAt length as he could come to no decision, he pulled off his boot,\nand said, take this boot, it has a hole in the sole of it. Go with\nit up to the loft, hang it on the big nail, and then pour water into\nit. If it hold the water, then I will again take a wife, but if it\nrun through, I will not. The girl did as she was bid, but the water\ndrew the hole toget [deleted the rest after class] >>> # Read 10 characters >>> myfile = open(filename,'r') >>> y = myfile.read(10) >>> y 'There was ' >>> z = myfile.read(25) >>> z 'once a man whose wife die' >>> # We can also read line by line >>> w = myfile.readline() >>> w 'd, and a woman whose husband\n' >>> l1 = myfile.readline() >>> l1 'died, and the man had a daughter, and the woman also had a\n' >>> l2 = myfile.readline() >>> l2 'daughter. The girls were acquainted with each other, and went\n' >>> myfile.close() >>> >>> # Detecting the end of the file while reading line by line >>> # First, let's use a while statement >>> myfile = open(filename) >>> line = myfile.readline() >>> while line != "": ... print line ... line = myfile.readline() ... There was once a man whose wife died, and a woman whose husband died, and the man had a daughter, and the woman also had a daughter. The girls were acquainted with each other, and went out walking together, and afterwards came to the woman in her house. Then said she to the man's daughter, listen, tell your [deleted the rest after class] >>> # why are we getting the extra blank lines? >>> # Because there is a \n at the end of the line of the file AND >>> # by default, print puts a \n at the end of the file. >>> # Here is a nonsense function to show that, and another option. >>> def stupid(): ... x = 'hi' ... y = 'there' ... print x ... print y ... print 'drats!' ... print x, ... print y, ... print 'drats!' ... >>> stupid() hi there drats! hi there drats! >>> #putting a , at the end of the print line prevents it from adding a new line >>> # we can also use a for loop to read a file >>> myfile.close() >>> myfile = open(filename) >>> for line in myfile: ... print line ... There was once a man whose wife died, and a woman whose husband died, and the man had a daughter, and the woman also had a daughter. The girls were acquainted with each other, and went out walking together, and afterwards came to the woman in her house. Then said she to the man's daughter, listen, tell your [deleted the rest after class] >>> for line in myfile: ... print line, ... >>> # we didn't get anything above, because we need to close and then >>> # open the file to read from it again >>> myfile.close() >>> myfile = open(filename) >>> for line in myfile: ... print line, ... There was once a man whose wife died, and a woman whose husband died, and the man had a daughter, and the woman also had a daughter. The girls were acquainted with each other, and went out walking together, and afterwards came to the woman in her house. Then said she to the man's daughter, listen, tell your father that I would like to marry him, and then you shall [deleted the rest] >>> # in the above, using the , prevented print from adding a \n >>> # remember strip? >>> x = " \t hui \n hjdks \n" >>> x ' \t hui \n hjdks \n' >>> print x hui hjdks >>> >>> x.strip() 'hui \n hjdks' >>> #so, another thing you can do to prevent the extra lines >>> myfile.close() >>> myfile = open(filename) >>> for line in myfile: ... print line.strip() ... There was once a man whose wife died, and a woman whose husband died, and the man had a daughter, and the woman also had a daughter. The girls were acquainted with each other, and went out walking together, and afterwards came to the woman in her house. Then said she to the man's daughter, listen, tell your father that I would like to marry him, and then you shall [deleted the rest] >>> # so, how to write? easy! >>> myfile.close() >>> fn = '/Users/janwiebe/Documents/temp.txt' >>> outfile = open(fn,'w') >>> outfile >>> outfile.write("This is a test.\n With a second line. \n") >>> outfile.close() >>> # now let's read from the file we created. >>> filename '/Users/janwiebe/Documents/Courses/CS0007/fairytale.txt' >>> fn '/Users/janwiebe/Documents/temp.txt' >>> myfile.open(fn) Traceback (most recent call last): File "", line 1, in AttributeError: 'file' object has no attribute 'open' >>> myfile = open(fn) >>> for line in myfile: ... print line, ... This is a test. With a second line. >>> >>> >>> Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] Type "help", "copyright", "credits" or "license" for more information. >>> # urllib is a module that lets you access webpages Evaluating slowdown.py 9577 >>> url = 'http://www.cs.pitt.edu' >>> url 'http://www.cs.pitt.edu' >>> d = urllib.urlopen(url) >>> for line in d: ... print line ... Department of Computer Science | University of Pittsburgh