我应该使用哪一个:os.sep或os.path.sep? [英] which one should I use: os.sep or os.path.sep?

查看:162
本文介绍了我应该使用哪一个:os.sep或os.path.sep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们是相同的,但是我应该使用哪一个?

They are same, but which one should I use?

http://docs.python.org/library/os.html :

os.sep

操作系统用来分隔路径名组件的字符.对于POSIX,这是"/";对于Windows,这是"\".请注意,仅知道这一点不足以解析或连接路径名-使用os.path.split()和os.path.join()-但这有时很有用.也可以通过os.path获得.

The character used by the operating system to separate pathname components. This is '/' for POSIX and '\' for Windows. Note that knowing this is not sufficient to be able to parse or concatenate pathnames — use os.path.split() and os.path.join() — but it is occasionally useful. Also available via os.path.

推荐答案

我将使用os.path.sep清楚地表明它是路径分隔符……但是一致性更重要,因此,如果已经使用了它,请使用那.否则,请选择一个并一直使用.

I'd use os.path.sep to make it very clear that it's the path separator… But consistency is more important, so if one is already being used, use that. Otherwise, pick one and use it all the time.

编辑:为了确保您没有重新发明轮子,尽管path模块已经具有joinsplitdirnamebasename功能…因此,您几乎不需要使用path.sep:

Edit: Just to make sure you're not reinventing the wheel, though, the path module already has join, split, dirname, and basename functions… So you should rarely need to use path.sep:

>>> os.path.join("foo", "bar", "baz")
'foo/bar/baz'
>>> os.path.split(_)
('foo/bar', 'baz')

这篇关于我应该使用哪一个:os.sep或os.path.sep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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