Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ea45c1e

Browse files
file exceptions example
1 parent b70233e commit ea45c1e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

‎FileExceptions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
try:
2+
my_file = open("mydata2.txt", encoding="utf-8")
3+
4+
except FileNotFoundError as ex:
5+
print("The file was not found")
6+
print(ex.args)
7+
8+
else:
9+
print("File: ", my_file.read())
10+
my_file.close()
11+
12+
finally:
13+
print("Finished working with file")
14+

‎mydata2.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is a file
2+
for an example
3+
that shows how to use exceptions
4+
with files in python.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /