title: os.rename error: The System Cannot Find The Path Specified
body: <p>I need to apply a new naming convention to files across a lot of subdirectories. For example, the files in one subdirectory might be:</p>
  
  <ul>
  <li><em>ABC (E) String of Text.txt</em></li>
  <li><em>ABC (E) String of Text.ocr.txt</em></li>
  <li><em>ABC (E) String of Text.pdf</em></li>
  </ul>
  
  <p>They need to all be renamed to follow this convention:</p>
  
  <ul>
  <li><em>ABC String of Text (E).txt</em></li>
  <li><em>ABC String of Text (E).ocr.txt</em></li>
  <li><em>ABC String of Text (E).pdf</em></li>
  </ul>
  
  <h2>Here's what I've got so far...</h2>
  
  <hr>
  
  <pre><code>import os, re
  
  regex = re.compile('\s\([a-zA-Z]+\)')
  
  path = os.path.expanduser('~/Google Drive/Directory/Subdirectory/')
  
  for files in os.walk(path):
      for name in files:
          strname = str(name)
          oldName = os.path.join(path,strname)
          if(regex.search(strname)):
              # identifying the token that needs shuffling
              token = regex.findall(oldName)
              # remove the token
              removed = (regex.split(oldName)[0] + ' ' +
                         regex.split(oldName)[1].strip())
              print removed # this is where everything goes wrong
              # remove the file extension
              split = removed.split('.')
              # insert the token at the end of the filename
              reformatted = split[0] + token[0]
              # reinsert the file extension
              for i in range(1,len(split)):
                  reformatted += '.' + split[i]
              os.rename(oldName,reformatted)
  </code></pre>
  
  <hr>
  
  <p>It ends up trying to rename the files by pulling a substring from a list of files in the directory, but includes list-related characters like "[" and "'", resulting in WindowsError: [Error 3] The system cannot find the path specified.</p>
  
  <p>Example:</p>
  
  <blockquote>
    <p>C:\Users\Me/Google Drive/Directory/Subdirectory/['ABC String of Text.txt', 'ABC</p>
  </blockquote>
  
  <p>My hope is that someone can see what I'm trying to accomplish and point me in the right direction.</p>