Renaming files in Python: No such file or directory
<p>If I try to rename files in a directory, for some reason I get an error.
I think the problem may be that I have not inserted the directory in the proper format ?</p>

<p>Additional info:
python 2 &amp;
linux machine</p>

<blockquote>
  <p>OSError: [Errno 2] No such file or directory</p>
</blockquote>

<p>Though it prints the directories content just fine. What am I doing wrong?</p>

<pre><code>import os

for i in os.listdir("/home/fanna/Videos/strange"):
    #print str(i)
    os.rename(i, i[:-17])
</code></pre>

<p><code>os.rename()</code> is expecting the full path to the file you want to rename. <code>os.listdir</code> only returns the filenames in the directory. Try this</p>

<pre><code>import os
baseDir = "/home/fanna/Videos/strange/"
for i in os.listdir( baseDir ):
    os.rename( baseDir + i, baseDir + i[:-17] )
</code></pre>

python