title: Check the permissions of a file in python
body: <p>I'm trying to check the readability of a file given the specified path. Here's what I have:</p>
  
  <pre><code>def read_permissions(filepath):
      '''Checks the read permissions of the specified file'''
      try:
          os.access(filepath, os.R_OK) # Find the permissions using os.access
      except IOError:
          return False
  
      return True
  </code></pre>
  
  <p>This works and returns True or False as the output when run. However, I want the error messages from <code>errno</code> to accompany it. This is what I think I would have to do (But I know that there is something wrong):</p>
  
  <pre><code>def read_permissions(filepath):
      '''Checks the read permissions of the specified file'''
      try:
          os.access(filepath, os.R_OK) # Find the permissions using os.access
      except IOError as e:
          print(os.strerror(e)) # Print the error message from errno as a string
  
      print("File exists.")
  </code></pre>
  
  <p>However, if I were to type in a file that does not exist, it tells me that the file exists. Can someone help me as to what I have done wrong (and what I can do to stay away from this issue in the future)? I haven't seen anyone try this using <code>os.access</code>. I'm also open to other options to test the permissions of a file. Can someone help me in how to raise the appropriate error message when something goes wrong?</p>
  
  <p>Also, this would likely apply to my other functions (They still use <code>os.access</code> when checking other things, such as the existence of a file using <code>os.F_OK</code> and the write permissions of a file using <code>os.W_OK</code>). Here is an example of the kind of thing that I am trying to simulate:</p>
  
  <pre><code>&gt;&gt;&gt; read_permissions("located-in-restricted-directory.txt") # Because of a permission error (perhaps due to the directory)
  [errno 13] Permission Denied
  &gt;&gt;&gt; read_permissions("does-not-exist.txt") # File does not exist
  [errno 2] No such file or directory
  </code></pre>
  
  <p>This is the kind of thing that I am trying to simulate, by returning the appropriate error message to the issue. I hope that this will help avoid any confusion about my question.</p>
  
  <p>I should probably point out that while I have read the <code>os.access</code> documentation, I am not trying to open the file later. I am simply trying to create a module in which some of the components are to check the permissions of a particular file. I have a baseline (The first piece of code that I had mentioned) which serves as a decision maker for the rest of my code. Here, I am simply trying to write it again, but in a user-friendly way (not just <code>True</code> or just <code>False</code>, but rather with complete messages). Since the <code>IOError</code> can be brought up a couple different ways (such as permission denied, or non-existent directory), I am trying to get my module to identify and publish the issue. I hope that this helps you to help me determine any possible solutions.</p>