Python Iterating through Directories and Renaming
<p>I am trying to iterate through a list of subdirectories and then opening the files within that subdirectory and renaming the files to lowercase. Here is my code:</p>

<pre><code>for root, subdirs, pics in os.walk(rootdir):
  for pic in pics:
    if pic.endswith('.jpg'):
        picpath = os.path.join(pic)
        #print pic
        print picpath
        #os.rename(pic, pic.replace(" ", "-").lower())
        os.rename(picpath, picpath.replace(" ", "-").lower())
</code></pre>

<p>I then get: </p>

<blockquote>
  <p><em>Traceback (most recent call last): File "imageresizing-renamefiles.py", line 19, in 
  os.rename(picpath, picpath.replace(" ", "-").lower()) OSError: [Errno
  2] No such file or directory</em></p>
</blockquote>

<p>My file structure is a root directory where the code runs from and within that folder are the following 
<code>folder1</code> with <code>Image1jpg</code> and <code>Image2jpg</code>, <code>folder2</code> with <code>Image3jpg</code> and <code>Image4jpg</code> and so on. I want to iterate through each to rename the files (not the folders) to lower case names.</p>

<p>Appreciate any help.</p>

<pre><code>picpath = os.path.join(root, pic)
                    #  ^^^^^
</code></pre>

<p>looks like it should do the job.  Per <a href="https://docs.python.org/2/library/os.html#os.walk" rel="nofollow">the docs</a>,</p>

<blockquote>
  <p>Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do <code>os.path.join(dirpath, name).</code></p>
</blockquote>

<p>That is why you are getting a "No such file" error: you are asking for the filename in the current directory, which isn't <code>root</code> at the point the error happens.</p>

python iteration rename