我现在有点困惑。我正在尝试重命名srt文件,但它只是工作了几次而且从未告诉我何时失败。此外,当我检查新文件是否存在时,它会说它确实存在,而稍后它会崩溃说新文件不存在。
这是重命名文件的代码:
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
这是使用上述功能的代码
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)
当我运行脚本时,它将此作为输出:
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 <module>
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'
在输出中有“file exists”当if语句检查重命名的文件是否存在时,只打印什么,因此该文件应该在当前工作目录中。但是,它仍然崩溃说文件在尝试移动时不存在,并且当我检查/ Volumes / media / Downloads文件夹时确实没有重命名该文件。有人可以告诉我这里出了什么问题吗?绝对奇怪的部分似乎是工作一段时间,而另一个时间它没有。就像它只是拒绝重命名文件而不告诉它没有这样做。
到目前为止,我尝试了多种选择。尝试一下:除了块没有帮助,我也在那里硬编码文件名,也导致有时重命名,有时不会。检查新文件是否存在是最新尝试使其工作,但使其表现更奇怪。
该脚本在raspberry pi上运行并在已安装的文件夹中工作。我检查了权限,这不是问题。