0

My program writes to a file using python. All goes well unless I already ran the file and kept the file open, throwing an IOError: [Errno 13] Permission denied: 'pathname'.

I'm wondering the best way to exit from the program and launch a message box to explain the issue to the user, instructing to close the previous file for overwrite.

def myFunction(self):
    with open(self.pathString, 'wb') as ofile:
    writer = csv.writer(ofile, quoting=csv.QUOTE_NONE, delimiter='\t')
    ...
    if IOError:
        QMessageBox.information(None, 'Information', 'A message')
        return
1
  • 1
    Please edit your question and post the relevant code in question. As a quick answer, though, always use the with context manager when opening files - that way they'll always be closed when you exit the processing loop. Commented Sep 30, 2014 at 19:16

1 Answer 1

1

It is easy:

try:
  result = do_here_what_can_cause_IOError
except IOError as e:
  log.error('Error when trying to perform do_here_what_can_cause_IOError: %s', e)
else:
  use_the_result_here

In a word, to catch any kind of exception Python throws, use try/except/else/finally

The documentation here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.