os.path.join [英] os.path.join

查看:67
本文介绍了os.path.join的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么os.path.join(''/ foo'',''/ bar'')返回''/ bar''而不是

''/ foo / bar'' ?这似乎相当直观。


Elliot

解决方案

5月1日,7日:下午36点,Elliot Peele< ell ... @bentlogic.netwrote:


为什么os.path.join(''/ foo'','' / bar'')返回''/ bar''而不是

''/ foo / bar''?这似乎相当直观。


Elliot



join(path1 [,path2 [,...]])

智能地加入一个或多个路径组件。如果任何组件是

绝对路径,那么所有以前的组件(在Windows上,包括

之前的驱动器号,如果有的话)都会被丢弃......


On Tue,2007-05-01 at 19:27 -0700,7stud写道:


5月1日晚上7点36分,Elliot Peele< ell ... @bentlogic.net写道:


为什么os.path.join(''/ foo'',''/ bar'')返回''/ bar''而不是

''/ foo / bar''?这似乎相当直观。


Elliot



join(path1 [,path2 [,...]])

智能地加入一个或多个路径组件。如果任何组件是一个

绝对路径,那么所有以前的组件(在Windows上,包括

之前的驱动器号,如果有的话)都会被丢弃...



是的,但是仍然没有回答我关于为什么os.path.join

以这种方式工作的问题。我明白这是怎么写的,但为什么呢?


Elliot


5月1日,9日:23 pm,Elliot Peele< ell ... @bentlogic.netwrote:


On Tue,2007-05-01 at 19:27 -0700,7stud写道:


5月1日晚上7:36,Elliot Peele< ell ... @bentlogic.netwrote:


为什么os.path.join(''/ foo'',''/ bar'')返回''/ bar''而不是

''/ foo / bar ''?这似乎相当直观。


Elliot


join(path1 [,path2 [,...]])

智能地加入一个或多个路径组件。如果任何组件是一个

绝对路径,那么所有以前的组件(在Windows上,包括

之前的驱动器号,如果有的话)都会被丢弃...



是的,但是仍然没有回答我关于为什么os.path.join

以这种方式工作的问题。我明白这就是它的写法,但为什么呢?


Elliot



这是完全合理的。您正在加入两个路​​径,这两个路径都从

根目录开始。第二条路径是覆盖第一条路径因为

它们既不能从根开始也可以是一条路径的一部分。


一个更好的问题是为什么这不起作用。


>> pathparts = [" / foo"," bar"]
os.path.join(pathparts)



[''/ foo' ',''bar'']


我认为这应该返回一个字符串。


~Sean


Why does os.path.join(''/foo'', ''/bar'') return ''/bar'' rather than
''/foo/bar''? That just seems rather counter intuitive.

Elliot

解决方案

On May 1, 7:36 pm, Elliot Peele <ell...@bentlogic.netwrote:

Why does os.path.join(''/foo'', ''/bar'') return ''/bar'' rather than
''/foo/bar''? That just seems rather counter intuitive.

Elliot

join( path1[, path2[, ...]])
Join one or more path components intelligently. If any component is an
absolute path, all previous components (on Windows, including the
previous drive letter, if there was one) are thrown away...


On Tue, 2007-05-01 at 19:27 -0700, 7stud wrote:

On May 1, 7:36 pm, Elliot Peele <ell...@bentlogic.netwrote:

Why does os.path.join(''/foo'', ''/bar'') return ''/bar'' rather than
''/foo/bar''? That just seems rather counter intuitive.

Elliot


join( path1[, path2[, ...]])
Join one or more path components intelligently. If any component is an
absolute path, all previous components (on Windows, including the
previous drive letter, if there was one) are thrown away...

Yes, but that still doesn''t answer my question as to why os.path.join
works that way. I understand that that is how it is written, but why?

Elliot


On May 1, 9:23 pm, Elliot Peele <ell...@bentlogic.netwrote:

On Tue, 2007-05-01 at 19:27 -0700, 7stud wrote:

On May 1, 7:36 pm, Elliot Peele <ell...@bentlogic.netwrote:

Why does os.path.join(''/foo'', ''/bar'') return ''/bar'' rather than
''/foo/bar''? That just seems rather counter intuitive.

Elliot

join( path1[, path2[, ...]])
Join one or more path components intelligently. If any component is an
absolute path, all previous components (on Windows, including the
previous drive letter, if there was one) are thrown away...


Yes, but that still doesn''t answer my question as to why os.path.join
works that way. I understand that that is how it is written, but why?

Elliot

It makes perfect sense. You are joining two paths that both begin at
the root directory. The second path is overwriting the first because
they can''t both begin at the root and also be parts of one path.

A better question is why this doesn''t work.

>>pathparts = ["/foo", "bar"]
os.path.join(pathparts)

[''/foo'', ''bar'']

This should return a string in my opinion.

~Sean


这篇关于os.path.join的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆