title: List of exceptions for Python3 I/O operations for proper error handling
body: <p>I'm trying to write to a file like this:</p>
  
  <pre><code>with open(filename), 'wb') as f:
      f.write(data)
  </code></pre>
  
  <p>But when opening a file and writing to it many things might go wrong and I want to properly report what went wrong to the user. So I don't want to do:</p>
  
  <pre><code>try:
      with open(filename), 'wb') as f:
          f.write(data)
  except:
      print("something went wrong")
  </code></pre>
  
  <p>But instead I want to tell the user more specifics so that they can fix the problem on their end. So I started with catching the <code>PermissionError</code> to tell the user to fix their permissions. But soon I got a bugreport that the program would fail if <code>filename</code> pointed to a directory, so I also caught and reported the <code>IsADirectoryError</code>. But this is not all - it might be that the path to <code>filename</code> doesn't exist, so later I also added the <code>FileNotFoundError</code>.</p>
  
  <p>Opening a file and writing to it is a very simple pattern but still many things can go wrong. I'm wondering whether there is an easy recipe that allows me to catch these errors and report them properly without giving a generic "some error happened" message. This kind of pattern has been used for decades, so there must be some list of things to check for, no?</p>
  
  <p>I now caught a number of exceptions but I can imagine that even more are lurking around. Things probably become even worse when running this on non-Linux platforms...</p>
  
  <p><strong>What are the best practices for opening a file and writing to it in Python that give a maximum of information back to the user when things go wrong without ressorting to dumping the bare traceback?</strong></p>
  
  <p>On the operating system level there are only so many things that can go wrong when doing an <code>open()</code> and <code>write()</code> system call so there must exist a complete list?</p>