i have a txt file i would like to use in my program named animallog1.txt. How do i open it so that it can be used in my program using try and except. I know how to use the file using open function but not using try except. Could someone tell me how it is done
You're very vague, but I'll try and answer your question. The reason you use try
and except
is to catch an error. In opening a file you would get an IOError
. So this is how you would open a file as such:
try:
with open("file.txt","r") as f:
#stuff goes here
except IOError:
#do what you want if there is an error with the file opening
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With