File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed
1 Python/docs/class_code_examples/fileIO Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Reader :
2
+
3
+ def __init__ (self , book_file ):
4
+ self .book_file = book_file
5
+ self .text = ''
6
+
7
+ def load (self ):
8
+ """
9
+ 1) open 'file' with option 'r' for read
10
+ 2) get the text from the file
11
+ """
12
+ with open (self .book_file , "r" ) as f_read :
13
+ self .text = f_read .read ()
14
+
15
+ return self .text
16
+
17
+ def edit (self , new_text ):
18
+ """
19
+ 1) open file with open 'w' for write
20
+ 2) take in additional text & concat onto existing file
21
+ 3) save to the file
22
+ """
23
+ with open (self .book_file , "a" ) as f_write :
24
+ f_write .write (new_text )
25
+
26
+ def replace (self , new_text ):
27
+ with open (self .book_file , "w" ) as f_write :
28
+ f_write .write (new_text )
29
+
30
+ def print (self ):
31
+ """
32
+ print the text
33
+ """
34
+
35
+
36
+ def main ():
37
+ text = Reader ('/Users/sb/Desktop/class_HB2/1 Python/docs/class_code_examples/fileIO/book.txt' ) # create an instance of our class
38
+ text .load ()
39
+ while True :
40
+ command = input ('Enter a command: ' )
41
+ if command == 'save' :
42
+ new_text = input ('add new text: ' )
43
+ text .save (new_text )
44
+ elif command == 'print' :
45
+ print ('heeey' )
46
+ else :
47
+ print ('Command not recognized' )
48
+
49
+
50
+ if __name__ == "__main__" :
51
+ main ()
Original file line number Diff line number Diff line change
1
+ David 5551234
2
+ Alice 6662345
Original file line number Diff line number Diff line change
1
+ name,phone number,state,zipe code
2
+ Sarah Beth,123432112,VA,22906
You can’t perform that action at this time.
0 commit comments