try except

To handle exception that you foresee, use try except

try:
    f=open('/tmp/xyz.txt')
    contents=f.read()
    f.close()
except:
    print("something wrong")

create a file

echo "hello world~wonderful" > /tmp/xyz.txt

Then run above Python code, then continue below

contents.strip('\n').split('~')
#['hello world', 'wonderful']

Last updated