This commit is contained in:
adii1823
2021-10-28 17:39:37 +05:30
parent 32c12d89f4
commit 48cef734e0
34 changed files with 796 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
# 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()