Files
Learn-Python-Programming-Th…/ch08/files/open_try.py
T
adii1823 48cef734e0 ch08
2021-10-28 17:39:37 +05:30

30 lines
480 B
Python

# files/open_try.py
fh = open('fear.txt', 'rt') # r: read, t: text
for line in fh.readlines():
print(line.strip()) # remove whitespace and print
fh.close()
# secured by try/finally
fh = open('fear.txt', 'rt')
try:
for line in fh.readlines():
print(line.strip())
finally:
fh.close()
# equivalent to:
fh = open('fear.txt') # rt is default
try:
for line in fh: # we can iterate directly on fh
print(line.strip())
finally:
fh.close()