Python In-Depth: Use Python Programming Features, Techniques, and Modules to Solve Everyday Problems by Ahidjo Ayeva & Kamon Ayeva & Aiman Saeed

Python In-Depth: Use Python Programming Features, Techniques, and Modules to Solve Everyday Problems by Ahidjo Ayeva & Kamon Ayeva & Aiman Saeed

Author:Ahidjo Ayeva & Kamon Ayeva & Aiman Saeed [Ayeva, Ahidjo & Ayeva, Kamon & Saeed, Aiman]
Language: eng
Format: epub
Publisher: BPB Publications
Published: 2021-08-15T00:00:00+00:00


f = None

try:

with open("testing_files/sample.txt") as f:

print("File object in the 'with' block:", f, sep="
")

print("Expect the file to be closed…")

print("… even when an exception occurs")

number = int("abc")

# Cannot actually see the next 2 print() results…

print("File object after the 'with' block…")

print("… but within a try/except:", f, sep="
")

except ValueError:

pass

print()

print("File object after the 'with' block:", f, sep="
")

print("Is the file closed?")

if f.closed:

print("Yes")

else:

print("No")

Executing this code gives the following output:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.