Python - IOError: [Errno 13] Permission denied
<p>Im trying to get a local directory from argv and iterate through the folder and print the contents of each file within. However i am getting a [Errno] 13 saying permission denied. Ive tried researching the problem but have come up empty handed.</p>

<pre><code>#!/usr/bin/python
import os
import sys


path = open(sys.argv[1],'r')   #'inputs/' path to working input dir
file_list = os.listdir(path)    #create list of filenames in path dir
for fn in file_list:
    file = open(path+'/'+fn)        #open each file in dir for manipulation
    for line in file:
        print(line)
</code></pre>

<p><a href="https://docs.python.org/3/library/os.html#os.listdir" rel="nofollow"><code>os.listdir()</code></a>, as its name implies, returns a list of all occupants of the given directory, including both files and directories (and, if you're on Unix/Linux, other stuff like symlinks and devices and whatnot). You are then blindly trying to <a href="https://docs.python.org/3/library/functions.html#open" rel="nofollow"><code>open()</code></a> each item in the list and <code>print()</code> its contents. Unfortunately, <code>open()</code> only works on file-like objects, and specifically does <em>not</em> work on directories, hence Errno 13, Permission Denied.</p>

<p>An alternative is to use <a href="https://docs.python.org/3/library/os.html#os.scandir" rel="nofollow"><code>os.scandir()</code></a>, which works a little bit differently. Instead of returning a flat list that you can read immediately, <code>os.scandir()</code> returns a <a href="http://stackoverflow.com/questions/1756096/understanding-generators-in-python">generator</a> which essentially gives you objects as you ask for them, instead of giving them all to you at once. In fact, the following code adapted from the docs is a good starting place for what you need:</p>

<pre><code>for entry in os.scandir(path):
    if entry.is_file():
       print(entry.name)
</code></pre>

<p><code>os.scandir()</code> is returning <a href="https://docs.python.org/3/library/os.html#os.DirEntry" rel="nofollow"><code>DirEntry</code></a> objects. Simply use <a href="https://docs.python.org/3/library/os.path.html#os.path.join" rel="nofollow"><code>os.path.join()</code></a> to create a full pathname out of the <code>path</code> argument you pass to <code>os.listdir()</code> in your original code, and <code>entry.name</code> from the code above, and then, using the <a href="https://docs.python.org/3/reference/compound_stmts.html#the-with-statement" rel="nofollow"><code>with</code></a> context manager, <code>open()</code> the file and display its contents:</p>

<pre><code>for entry in os.scandir(path):
    if entry.is_file():
       with open(os.path.join(path, entry), "r") as f:
           for line in f:
               print(line)
</code></pre>

<p>One of the advantages of using <code>with</code> is that you don't have to remember to close the file handle that is assigned when you use something like this:</p>

<pre><code>f = open("myfile.txt, "r")
# do stuff with f
...
f.close()
</code></pre>

<p>Otherwise, you have a dangling file handle that <em>could</em> potentially cause problems, depending on how many there are and what you've done with them. It's just a good practice to <code>close()</code> what you <code>open()</code>. With <code>with</code>, you don't have to worry about it - the file handle is closed as soon as you exit the block.</p>

python