Lec13FilesReview

Lec13FilesReview - Files(Chap 5 Review for exam We want the...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Files (Chap 5), Review for exam We want the ability to read and write files. Today, we will only do reading. 1) Get the name of the file – how about if we ask the user 2) Open the file 3) Read in the file, one line at a time 4) Close the file # ask user for a filename filename = input(“Please enter a filename: “) # open the file inFile = open(filename, “r”) # “r” means open for reading # read in each line of the file for eachLine in inFile: print(eachLine) # when I’m done, close the file inFile.close() At the end of each line of a file, if it is a text file, not MS Word or something more fancy, there is a “newline” character. ‘\n’ – newline ‘\t’ – tab Problem: You have a list of strings. What you want is a dictionary in which the items are paired by {string[0]:string[1], string[2]:string[3], string[4]:string[5]} myDictionary = {“Cat”:”Mouse”,”Dog”:”Cat”, ”OlderBrothers”:”YoungerBrothers”} myList = [“Cat”,”Mouse”,”Dog”,”Cat”,”OlderBrothers”,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/27/2011 for the course CMPSC 8 taught by Professor Franklin during the Fall '09 term at UCSB.

Page1 / 2

Lec13FilesReview - Files(Chap 5 Review for exam We want the...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online