python教程—我应该使用哪个:os。9月还是os.path.sep ?-Python实用宝典

python教程—我应该使用哪个:os。9月还是os.path.sep ?

它们是相同的,但是我应该使用哪个呢?这是'/'的POSIX和''的Windows。注意,知道这不是足够的能够解析或连接路径名€”使用os.path.split()和os.path.join()一个€”但偶尔有用。也可以通过os.path获得。

它们是一样的,但是我应该用哪一个呢?

< a href = " http://docs.python.org/library/os.html " rel = " noreferrer " > http://docs.python.org/library/os.html < / >:

os.sep

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.

回答

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.

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')

​Python实用宝典 (pythondict.com)
不只是一个宝典
欢迎关注公众号:Python实用宝典

本文由 Python实用宝典 作者:Python实用宝典 发表,其版权均为 Python实用宝典 所有,文章内容系作者个人观点,不代表 Python实用宝典 对观点赞同或支持。如需转载,请注明文章来源。
0

发表评论