Python: os.listdir files are not found
<p>I recently moved my config files to another folder in my Project. I try to load the like this:</p>

<pre><code>CONFIG_PATH = os.path.abspath(os.path.dirname(os.path.abspath(__file__))+"/../config/")    

def load_config():
    configs = {}
    for config in os.listdir(CONFIG_PATH):
        configs[str(config)[0:-12]] = json.load(open(config))
    return configs
</code></pre>

<p>I'm running the code from</p>

<pre><code>D:/.../MyProject/src
</code></pre>

<p>And the specified <code>CONFIGPATH</code> is correctly set to</p>

<pre><code>D:/.../MyProject/config
</code></pre>

<p>Now in that iteration loop, the <code>open(config)</code> raises an exception:</p>

<pre><code>FileNotFoundError: [Errno 2] No such file or directory: 'sample.config.json'
</code></pre>

<p>I can't see why my program can't open a file, which clearly has to exist since it is given out by <code>os.listdir</code>. Actually, a <code>print(config)</code> in the loop confirms that there is a file with that name. So why won't it open and instead raises a FileNotFoundError?</p>

<p>Do I miss the obvious here? The code worked before I moved the files upwards. I'm working with Pycharm on Windows 7, if that is of any relevance.</p>

<p><code>os.listdir</code> only return the name of the file, not the complete path.</p>

<p>If you're on 3.5 you can use <code>os.scandir</code> where the returned item has a path attribute. If you're not that lucky, you'll have to construct the full path yourself.</p>

<p>It would be: <code>json.load(open(os.path.join(CONFIG_PATH, config)))</code> in your case.</p>

python json file