我是通勤时间程序员,所以请保持温和。现在针对实际问题,我的一个用户遇到了这种奇怪的行为,其中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")
处于/ True状态,而它似乎不适合我的用户。有了这个,我可以成功地重现这个bug。我将发布该修复程序并让有问题的用户确认它有效,但到目前为止,它都指向save_path
是罪魁祸首。也觉得有点像一个白痴俯视明显,真的。无论如何,谢谢,我会回复你的结果。
答案 0 :(得分:5)
查看save_path
:
if os.name == 'nt':
return remove_chars(name, '/\\?%*:|"<>')
else:
return remove_chars(name, '/\\"')
此函数删除/
(以及其他一些在文件名中有特殊含义的东西)。所以..这就是删除斜线的原因。
我猜这个功能是为了清理用户上传文件的文件名(因此他们无法上传名为../../../etc/passwd
的文件)。如果没有必要,那么解决方案就是省略对此函数的调用。