python - os.path.join(p1,p2) 省略 p1 的斜杠

标签 python slash os.path

我是业余程序员,所以请温柔一点。现在,对于实际问题,我的一个用户遇到了这种奇怪的行为,其中 os.path.join(p1,p2) 返回一个相对路径,其中省略了 p1 中的所有斜杠。像这样(假装这是在 python 命令行解释器中完成的):

>>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 命令行解释器执行 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 的开源项目撰写文章。只是想确保我没有声称任何东西是我的代码,而实际上它不是。

编辑:布伦丹·朗的回答似乎是准确的。在我的开发环境中,self.getConfig("subfolder") 为 on/True,但看起来它不适合我的用户。这样我就可以成功地重现该错误。我将发布该修复程序并让相关用户确认其有效,但到目前为止,一切都表明 save_path 是罪魁祸首。真的,因为忽视了显而易见的事情,我也觉得自己有点像个白痴。不管怎样,谢谢,我会告诉你结果。

最佳答案

查看save_path:

if os.name == 'nt':
    return remove_chars(name, '/\\?%*:|"<>')
else:
    return remove_chars(name, '/\\"')

此函数删除 /(以及文件名中具有特殊含义的其他一些内容)。所以..这就是删除斜线的原因。

我猜这个函数是为了清理用户上传文件的文件名(这样他们就无法上传名为../../../etc/passwd的文件)。如果没有必要,那么解决方案就是省略对此函数的调用。

关于python - os.path.join(p1,p2) 省略 p1 的斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16953223/

相关文章:

python - 无法通过按键盘上的 Q 键来结束游戏

url - 禁止路径末尾的斜线是什么意思?

python - 当给定实际现有目录的路径时 os.path.isdir 不返回 True

python - 为 SQLAlchemy 批量更新插入寻找更好的策略

python - 数据库错误 : ORA-01036: illegal variable name/number

json - 为什么斜杠是 JSON 中的可转义字符?

python - Google 应用程序引擎 os.path 功能不起作用

python - 避免递归树中的基本路径

python - pandas cut(): how to convert nans?还是将输出转换为非分类?

php - Ajax : how to send backslash in data?