Difference between revisions of "CSC111 Lab 5 2015"
(Created page with "--~~~~ ----") |
|||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 12:17, 22 February 2015 (EST) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 12:17, 22 February 2015 (EST) | ||
---- | ---- | ||
+ | |||
+ | <showafterdate after="20150225 11:00" before="20150601 00:00"> | ||
+ | |||
+ | =Preparation= | ||
+ | <br /> | ||
+ | * Copy paste the program below in a new Idle window, and call it '''lab5Companion.py'''. Save it to your working directory, where you normally save your lab programs. | ||
+ | * Run the program. It should create 3 text files in your folder. You will be using them in this lab. The files are called: | ||
+ | ** chocoQuotes.txt | ||
+ | ** hargitay.txt | ||
+ | ** joanneHarris.txt | ||
+ | |||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # lab5Companion.py | ||
+ | # D. Thiebaut | ||
+ | # This program will create several files in the directory | ||
+ | # where you will run it. | ||
+ | # The files are retrieved from a Web server, stored temporarily | ||
+ | # in a string, and stored in a local file on your disk. | ||
+ | # The 3 files are very, very short. | ||
+ | # Their names are: | ||
+ | # - chocoQuotes.txt | ||
+ | # - hargitay.txt | ||
+ | # - joanneHarris.txt | ||
+ | |||
+ | import urllib.request # the lib that handles the url stuff | ||
+ | |||
+ | # main program | ||
+ | def main(): | ||
+ | |||
+ | # list of files to retrieve | ||
+ | fileNames = [ "chocoQuotes.txt", "hargitay.txt", "joanneHarris.txt" ] | ||
+ | |||
+ | # get each file, one at a time | ||
+ | for fileName in fileNames: | ||
+ | |||
+ | # url where the files are located | ||
+ | url = "http://cs.smith.edu/~dthiebaut/111/files/" + fileName | ||
+ | |||
+ | # fetch the file and put its contents in the | ||
+ | # strign text. | ||
+ | response = urllib.request.urlopen( url ) | ||
+ | text = response.read().decode( 'UTF-8' ) | ||
+ | |||
+ | # write text to file, in the same directory as this program. | ||
+ | file = open( fileName, "w" ) | ||
+ | file.write( text + "\n" ) | ||
+ | file.close() | ||
+ | |||
+ | # give feedback to user. | ||
+ | print( "File {0:1} created in your folder".format( fileName ) ) | ||
+ | |||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | <br /> | ||
+ | </showafterdate> |
Revision as of 18:56, 22 February 2015
--D. Thiebaut (talk) 12:17, 22 February 2015 (EST)
<showafterdate after="20150225 11:00" before="20150601 00:00">
Preparation
- Copy paste the program below in a new Idle window, and call it lab5Companion.py. Save it to your working directory, where you normally save your lab programs.
- Run the program. It should create 3 text files in your folder. You will be using them in this lab. The files are called:
- chocoQuotes.txt
- hargitay.txt
- joanneHarris.txt
# lab5Companion.py # D. Thiebaut # This program will create several files in the directory # where you will run it. # The files are retrieved from a Web server, stored temporarily # in a string, and stored in a local file on your disk. # The 3 files are very, very short. # Their names are: # - chocoQuotes.txt # - hargitay.txt # - joanneHarris.txt import urllib.request # the lib that handles the url stuff # main program def main(): # list of files to retrieve fileNames = [ "chocoQuotes.txt", "hargitay.txt", "joanneHarris.txt" ] # get each file, one at a time for fileName in fileNames: # url where the files are located url = "http://cs.smith.edu/~dthiebaut/111/files/" + fileName # fetch the file and put its contents in the # strign text. response = urllib.request.urlopen( url ) text = response.read().decode( 'UTF-8' ) # write text to file, in the same directory as this program. file = open( fileName, "w" ) file.write( text + "\n" ) file.close() # give feedback to user. print( "File {0:1} created in your folder".format( fileName ) ) main()
</showafterdate>