Error 3: Renaming files in python
<p>Newbie Python question. </p>

<p>I'm trying to rename files in a directory...</p>

<p>the value of path is </p>

<pre><code>C:\tempdir\1\0cd3a8asdsdfasfasdsgvsdfc1.pdf
</code></pre>

<p>while the value newfile is </p>

<p><code>C:\tempdir\1\newfilename.pdf</code> </p>

<pre><code>origfile = path
newfile = path.split("\\")
newfile = newfile[0]+"\\"+newfile[1]+"\\"+newfile[2]+"\\"+text+".pdf"

os.rename(path, newfile)
print origfile
print newfile
</code></pre>

<p>im getting the following error...</p>

<pre><code>os.rename(path, newfile)
WindowsError: [Error 3] The system cannot find the path specified
</code></pre>

<p>I know the directory and file are good because i can call os.stats() on it.  I have changed to value of newfile to include the new file name only but recieve the same error (after reading the python documentation on rename())</p>

<p>My imported libraries are....</p>

<pre><code>import sys
import os
import string
from os import path
import re
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO
</code></pre>

<p>I've read some other threads on this topic - pertaining to absolute vs. relative paths.  Obviously, my intent is to use absolute paths.  My variables are string variable, anotherwords...</p>

<pre><code>origfile = "C:\tempdir\1\0cd3a8asdsdfasfasdsgvsdfc1.pdf"
</code></pre>

<p>Is that enough? or am i supposed to be using some other declaration to tell python this is a path?  </p>

<p>You should better use <code>ntpath</code> (explained <a href="http://stackoverflow.com/questions/8384737/python-extract-file-name-from-path-no-matter-what-the-os-path-format">here</a>) to modify only your filename:</p>

<pre><code>&gt;&gt;&gt; filepath = 'C:\\tempdir\\1\\0cd3a8asdsdfasfasdsgvsdfc1.pdf'
&gt;&gt;&gt; dirname, filename = ntpath.dirname(filepath), ntpath.basename(filepath)
&gt;&gt;&gt; dirname
'C:\\tempdir\\1'
&gt;&gt;&gt; filename
'0cd3a8asdsdfasfasdsgvsdfc1.pdf'
</code></pre>

<p>Hence, you will probably be able to use rename as follows:</p>

<pre><code>&gt;&gt;&gt; os.rename(filepath, dirname + ntpath.sep + 'newfilename.pdf')
</code></pre>

<p>Using <code>ntpath.sep</code> uses the appropriate separator.</p>

python