Good day!
i have a code below:
def initial(*args):
for arg in args:
with open(arg) as f:
print 'passed'
it called from this code:
if __name__ == '__main__':
initial('test_staff/1.txt', 'test_staff/d2.txt', 'test_staff/1.txt')
My question is, if second parametr ‘test_staff/d2.txt’ broken(file not exist), how to continue execute a function(with third param)?
I see several methods to do it:
- write function to pass existance
- use try, throw, finally.
but how it perform with “with statement from pep” ?
Thank you!
upd: function name changed from __initial__() to initial()
I’d suggest to check whether the file exists or doing some try / except
Otherwise you could just catch the exception (
IOError)Some people say “Try and Catch” instead of doing many checks, there are others that prefer checking instead of trying and catching! IMHO, both of them are fine!