@@ -21,7 +21,7 @@ def __eq__(self, other):
2121 #It's approriate to give something for __hash__ when you override __eq__
2222 # #This is the recommended way if mutable (like it is here):
2323 __hash__ = None
24-
24+
2525
2626########## Passing by Object Reference ##########
2727
@@ -63,5 +63,93 @@ def modify(book):
6363
6464
6565########## Reading from a file ##########
66- ########## Context manager (as?) ##########
67- ########## Intro to Exception handling ##########
66+
67+ file = open ("input.txt" , "a" ) #append. Creates if not there
68+ file = open ("input.txt" , "w" ) #overwrites
69+ #file = open("input.txt", "r+") #Read and write. Throws exception if not there
70+ #not as easy to work with on r/w in my oppinion
71+
72+ #\t to sep title from page count
73+ file .write ("Are You My Mother?\t 72\n " )
74+ file .write ("The Digging-est Dog\t 72" )
75+ file .close () #close when done
76+
77+ file = open ("input.txt" , "r" )
78+
79+ #for line in file:
80+ # print(line, end="") #end="" as \n is kept in line
81+ #print()
82+
83+ #Numerous ways to read into list. Here is one.
84+ data = file .read ().split ('\n ' )
85+ print (data )
86+ #https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list
87+
88+ file .close ()
89+
90+ #Each line is title\tpages so we split it.
91+ book1_data = data [0 ].split ('\t ' )
92+ book = Book (book1_data [0 ], book1_data [1 ])
93+
94+ book2_data = data [1 ].split ('\t ' )
95+ book2 = Book (book2_data [0 ], book2_data [1 ])
96+
97+ print (book )
98+ print (book2 )
99+
100+
101+ ########## Intro to Exception handling ##########
102+
103+
104+ #When working with files (or doing anything in programming),
105+ #Exceptions can be thrown
106+
107+ #file = open("doesntexist.csv", "r") #get wrecked son
108+
109+ try :
110+ file = open ("doesntexist.csv" , "r" )
111+ except Exception as e :
112+ print (type (e ))
113+ print (e )
114+
115+ try :
116+ file = open ("input.txt" , "r" ) #Make sure it exists
117+ data = int (file .read ()) #This should fail
118+ except FileNotFoundError as e :
119+ print ("This file is not found" )
120+ except PermissionError as e :
121+ print ("file is locked" )
122+ except ValueError as e :
123+ print ("Cannot parse data. Check file" )
124+ except Exception as e :
125+ print (type (e ))
126+ print (e )
127+ finally :
128+ file .close ()
129+ print ("Always runs" )
130+
131+
132+ ########## with keyword ##########
133+
134+ #shorthand for opening file and automatically closed outside indent
135+ with open ("input.txt" , "r" ) as file : #Make sure it exists
136+
137+ try :
138+ int (file .read ())
139+ except : #example of using thin except here
140+ print ("parse error...etc..." )
141+ #do whatever
142+
143+ print (file .closed ) #closed
144+
145+ #opening can still throw an exception...Maybe do it like so:
146+ try :
147+ with open ("no" , "r" ) as file : #Make sure it exists
148+ try :
149+ int (file .read ())
150+ except :
151+ print ("parse error...etc..." )
152+ #do whatever
153+
154+ except Exception as e :
155+ print (e )
0 commit comments