Class Notes (835,600)
Canada (509,275)
CSC108H1 (113)
Lecture

october 24 notes

3 Pages
72 Views
Unlock Document

Department
Computer Science
Course
CSC108H1
Professor
Michelle Craig
Semester
Fall

Description
 Text file treated as an import o If file not in same directory  full directory given instead ex. ‘c:\\users\\folder\\file’  Can only interact with file in one way at a time, read ‘r’ or write ‘w’ or append ‘a’ o Must close file before switching between two interaction types >>> filename = raw_input("please enter the name of a file") please enter the name of a filestory.txt >>> filename 'story.txt' # only works if its in the same directory >>> type(filename) >>> open(filename,'r') # entering the filename as a string also works o File assigned to a variable, not the texts inside >>> my_story = open(filename,'r') # r = read >>> type(my_story) o Reader starts at the beginning of the file o Readline gives the string in the line in the shell o Readline continues the reader to next line, stays there  resets to begining when the variable the file is assigned to is called & file opened >>> line = my_story.readline() >>> type(line) >>> line 'Once upon a time there was a curious little girl\n' >>> line = my_story.readline() # call my_story.readline() would move the reader down >>> line 'named Goldilocks. Goldilocks lived near the woods\n' >>> line = my_story.readline() >>> line 'and loved to go exploring.\n' >>> my_story #reader resets, no need to close first >>> line = my_story.readline() >>> line 'Once upon a time there was a curious little girl\n' >>> my_story.readline() '\n' # indicts a blank line, ie. header or line btwn paragraphs >>> line = my_story.readline() >>> line = my_story.readline() >>> line = my_story.readline() >>> line = my_story.readline() >>> line '' # gives blank string when exceed the length of the file, ie. out of lines to read >>> my_story.close()  read character by character >>> f = open('story.txt','r') >>> bit = f.read(10) #first ten characters >>> bit 'Once upon ' >>> bit = f.read(20) #first 20 th characters after the first ten, reader moved >>> bit 'a time there was a c' >>> bit = f.read(20) >>> bit 'urious little girl\nn' >>> print bit # note the difference when its called in shell and when its prints urious little girl n >>> f.close()  for loop with text file ex.read the whole file one line at a time >>> f = open('story.txt') >>> for line in f: ... print line ... # do whatever you want with str line ... Once upon a time there was a curious little girl named Goldilocks. Goldilocks lived near the woods and loved to go exploring. One day Goldilocks came upon a c
More Less

Related notes for CSC108H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit