我是业余程序员,所以请温柔。现在对于实际问题,我的一个用户遇到了这种奇怪的行为,其中os.path.join(p1,p2)
返回一个相对路径,其中省略了所有斜杠p1
。像这样(假装这是在 python cmd 行解释器中完成的):
>>import os
>>p1 = "/Some/Path/Tosmth"
>>p2 = "file.ext"
>>print os.path.join(p1,p2)`
然后输出是:
>>"SomePathTosmth/file.ext"
就在加入操作之前,我检查了 p1 和 p2 的内容,这正是我所期望的。这是有问题的实际实现以及一些额外的调试代码:
def __moveMovie(self, src, dst, folder, file_name):
try:
self.logDebug('__moveMovie(): src=%s | dst=%s | folder=%s | file_name=%s' % (src, dst, folder, file_name))
dest = save_path(dst)
file_name = save_path(file_name)
if self.getConfig("subfolder") is True:
dest = os.path.join(dst,folder)
os.mkdir(Utils().encode(dest))
except OSError, e:
if e.args[0] == 17:
self.logDebug(u'Cannot create folder "%s". It already exists.' % os.path.join(dest))
try:
full_dst = Utils().encode(os.path.join(dest,file_name))
self.logDebug('var "full_dst" w/o encode: %s' % os.path.join(dest, file_name))
self.logDebug('var "full_dst" w/ encode: %s' % Utils().encode(os.path.join(dest,file_name)))
if os.path.exists(full_dst):
pass
shutil.move(src, full_dst)
self.logInfo(u'Movie "%s" moved to "%s"' % (os.path.split(src)[1], os.path.join(dest,file_name)))
self.__movie_queue.task_done()
except OSError, e:
if e.args[0] == 21:
self.logDebug(u'Cannot move "%s" to "%s". "%s" is a directory.' % (os.path.split(src)[1],
os.path.join(dest, file_name),
os.path.join(dest, file_name)))
self.__movie_queue.task_done()
这是该代码的日志:
05.06.2013 17:29:12 DEBUG MovieMover: __moveMovie(): src=/var/raid/Daten/Neu/abgezockt.tc.72-ps/Voll.Abgezockt.2013.UNRATED.GERMAN.AC3LD.5.1.DL.720p.BluRay.x264-DerSchuft.mkv | dst=/var/raid/Daten/Filme | folder=Voll.Abgezockt.2013.UNRATED.GERMAN.AC3LD.5.1.DL.720p.BluRay.x264-DerSchuft | file_name=Voll Abgezockt.mkv
05.06.2013 17:29:12 DEBUG MovieMover: var "full_dst" w/o encode: varraidDatenFilme/Voll Abgezockt.mkv
05.06.2013 17:29:12 DEBUG MovieMover: var "full_dst" w/ encode: varraidDatenFilme/Voll Abgezockt.mkv
05.06.2013 17:29:12 DEBUG MovieMover: __moveMovie(): src=/var/raid/Daten/Neu/abgezockt.tc.72-ps/.AppleDouble/Voll.Abgezockt.2013.UNRATED.GERMAN.AC3LD.5.1.DL.720p.BluRay.x264-DerSchuft.mkv | dst=/var/raid/Daten/Filme | folder=Voll.Abgezockt.2013.UNRATED.GERMAN.AC3LD.5.1.DL.720p.BluRay.x264-DerSchuft | file_name=Voll Abgezockt.mkv
05.06.2013 17:29:12 DEBUG MovieMover: var "full_dst" w/o encode: varraidDatenFilme/Voll Abgezockt.mkv
05.06.2013 17:29:12 DEBUG MovieMover: var "full_dst" w/ encode: varraidDatenFilme/Voll Abgezockt.mkv
Exception in thread Thread-1072:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "/var/local/pyload/userplugins/hooks/MovieMover.py", line 360, in __getMvQueue
self.__moveMovie(src, dst, movie.folder_name, movie.file_name)
File "/var/local/pyload/userplugins/hooks/MovieMover.py", line 382, in __moveMovie
shutil.move(src, full_dst)
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 83, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 2] No such file or directory: u'varraidDatenFilme/Voll Abgezockt.mkv'
Exception in thread Thread-1074:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "/var/local/pyload/userplugins/hooks/MovieMover.py", line 360, in __getMvQueue
self.__moveMovie(src, dst, movie.folder_name, movie.file_name)
File "/var/local/pyload/userplugins/hooks/MovieMover.py", line 382, in __moveMovie
shutil.move(src, full_dst)
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 83, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 2] No such file or directory: u'varraidDatenFilme/Voll Abgezockt.mkv'
我完全不知所措,因为包含 os.path.join() 函数的编码函数不是原因,当他使用他的 python cmd 行解释器执行 os.path.join() 时,结果是正确的。如果有任何兴趣,我正在 2.5 中编写,而他正在 2.7.2 上执行代码。我希望有人能够对此有所了解。谢谢!
由于看起来我不能在评论中使用代码格式,我将在这里回答:
这是 save_path() 函数:
def save_path(name):
#remove some chars
if os.name == 'nt':
return remove_chars(name, '/\\?%*:|"<>')
else:
return remove_chars(name, '/\\"')
现在我必须添加的不是我的代码。我正在为一个名为 pyLoad 的开源项目写作。只是想确保我没有声称任何东西是我的代码,而实际上它不是。
编辑:Brendan Long 的回答似乎是准确的。在我的开发环境self.getConfig("subfolder")
中是 on/True,而它似乎不适合我的用户。这样我就可以成功地重现该错误。我将发布该修复程序并让有问题的用户确认它有效,但到目前为止,这一切都指向save_path
了罪魁祸首。也感觉有点像个白痴,因为忽略了显而易见的事情,真的。无论如何,谢谢,我会回复你的结果。