title: Renaming file in same directory using Python
body: <p>So I'm trying to iterate through a list of files that are within a subfolder named <code>eachjpgfile</code> and change the file from <code>doc</code> to the subfolder <code>eachjpgfile</code> mantaining the file's name but when I do this it adds the file to directory before <code>eachjpgfile</code> rather than keeping it in it.  Looking at the code below, can you see why is it doing this and how can I keep it in the <code>eachjpgfile</code> directory?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>for eachjpgfile in filelist:
      os.chdir(eachjpgfile)
      newdirectorypath = os.curdir
      list_of_files = os.listdir(newdirectorypath)
      for eachfile in list_of_files:
           onlyfilename = os.path.splitext(eachfile)[0]
           if onlyfilename == 'doc':
              newjpgfilename = eachfile.replace(onlyfilename,eachjpgfile)
              os.rename(eachfile, newjpgfilename)
  </code></pre>