os.rename not working python
<p>I'm sort of confused right now. I'm trying to rename srt files, but it somehow only works a few times and never tells me when it fails. Also, when I check if the new file exists it says it does, while later on it crashes saying the new file does not exists.</p>

<p>This is the code that renames the file:</p>

<pre><code>def renameFile(self):
    oldPath = os.getcwd()
    os.chdir(self.downloadPath)
    fileNameNoEx, ex = os.path.splitext(self.fileName)
    newName = fileNameNoEx+".nl"+ex
    os.rename(self.fileName, newName)
    if os.path.isfile(newName):
        print newName
        print "file exists"
        os.chdir(oldPath)
        return newName
    else:
        os.chdir(oldPath)
        self.l.error("Something went wrong while trying to rename %s" % self.fileName)
        return False
</code></pre>

<p>And this is the code using the function above</p>

<pre><code>def move(self):
    info = self.getInfo(self.fileName)
    path = self.getMoveToPath(info)
    if path:
        if self.subtitle:
            self.fileName = self.renameFile() #HERE IT IS!
            if not self.fileName:
                return False
        self.makeDest(path)
        self.l.info("moving %s to %s" % (self.fileName, path))
        completePath = os.path.join(path, self.fileName)
        if not os.path.isfile(completePath):
            self.fileName = os.path.join(self.downloadPath, self.fileName)
            print path
            shutil.move(self.fileName, path)
        else:
            self.l.info("File Already exsists")
    else:
        self.l.error("No path to move %s to" % self.fileName)
</code></pre>

<p>When I run the script, it gives this as output:</p>

<pre><code>Do.No.Harm.S01E03.720p.HDTV.X264-DIMENSION.nl.srt
file exists
/Volumes/media/Series/Do No Harm/Season 1/S01E03
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/pi/apps/manager/__main__.py", line 7, in &lt;module&gt;
    m.manage()
  File "manager/manager.py", line 37, in manage
    self.move()
  File "manager/manager.py", line 53, in move
    shutil.move(self.fileName, path)
  File "/usr/lib/python2.7/shutil.py", line 301, in move
    copy2(src, real_dst)
  File "/usr/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: '/Volumes/media/Downloads/Do.No.Harm.S01E03.720p.HDTV.X264-DIMENSION.nl.srt'
</code></pre>

<p>In the output there is "file exists" What's only printed when the if statement that checks if the renamed file exists, so the file should be in the current working directory. But, it still crashes saying that the file does not exists when trying to move it, and the file is indeed not renamed when i check the /Volumes/media/Downloads folder. Could someone please tell me what is going wrong here? The absolute weird part is that is seems to work some time, and another time it doesn't. Like it justs refuses to rename the file without telling that it didn't do it. </p>

<p>I've tried multiple options so far. Putting it in a try: Except block won't help and I also hardcoded the file name's in there, also resulting in sometimes renaming and sometimes not. The check if the new file exists is the newest try on getting this to work, but making it behave even weirder. </p>

<p>The script is running on a raspberry pi and working in a mounted folder. I checked permissions and this was not the problem.</p>
