How to loop over directories and rename files?
<p>Im ney to python and therefore apologize for the triviality of my question:</p>

<p>I have the following file structure, where a <code>.csv</code> file with employee is saved on daily basis:</p>

<pre><code>dir/2012-01-01/employee.csv.bz2
dir/2012-01-02/employee.csv.bz2
dir/2012-01-03/employee.csv.bz2
dir/2012-01-04/employee.csv.bz2
dir/2012-01-05/employee.csv.bz2
</code></pre>

<p>I would like to go through each file and rename it. Afterwards I would like to save the new files in one common directory <code>dir/common</code>. What I tried:</p>

<pre><code>import sys
import os


path = 'dir/'

for folderName, subfolders, filenames in os.walk(path):
    for filename in filenames:
        infilename = os.path.join(path, filename)
        newname = infilename.replace('.csv.bz2', '.csv')
        output = os.rename(infilename, newname)
</code></pre>

<p>But I get the error:</p>

<pre><code>output = os.rename(infilename, newname)
FileNotFoundError: [WinError 2] The system cannot find the file specified:   'dir/employee.csv.bz2' -&gt; 'dir/employee.csv'
</code></pre>

<p>Not sure what Im doing wrong.</p>