Create a file to read from
|
1 |
$ vi person.txt |
|
1 2 3 4 5 |
John, 19, student Peter, 25, engineer Sam, 45, manager Sally, 36, lawyer |
Read from a file ‘r’ is for open file in read mode.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ python3 >>> f = open('person.txt', 'r') >>> type(f) <type 'file'> >>> print(f.read()) John, 19, student Peter, 25, engineer Sam, 45, manager Sally, 36, lawyer >>> f.close() >>> |
handle exception & close file close( ) the file in the finally block, which will be executed even if…