try except
To handle exception that you foresee, use try except
1
try:
2
f=open('/tmp/xyz.txt')
3
contents=f.read()
4
f.close()
5
except:
6
print("something wrong")
7
​
8
​
Copied!
create a file
echo "hello world~wonderful" > /tmp/xyz.txt
Then run above Python code, then continue below
1
contents.strip('\n').split('~')
2
#['hello world', 'wonderful']
Copied!
Last modified 1yr ago
Copy link