which one should I use: os.sep or os.path.sep?

后端 未结 4 1511
心在旅途
心在旅途 2020-12-13 16:48

They are same, but which one should I use?

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

os.sep

The character used by t

相关标签:
4条回答
  • 2020-12-13 17:42

    If you are using Jython 2.7, I suggest using os.sep (works) instead of os.path.sep (broken) as Jython on Windows has a bug returning a "/" slash instead of the required "\" backslash.

    0 讨论(0)
  • 2020-12-13 17:44

    I recommend you use os.path.sep for clarity, since it's a path separator, not an OS separator. If you import os.path as path you can call it path.sep, which is even better.

    0 讨论(0)
  • 2020-12-13 17:47

    The following examples could highlight the differences between os.path.join and os.path.sep.join.

    import os os.path.join("output", "images", "saved") 'output/images/saved' os.path.sep.join(["output", "images", "saved"]) 'output/images/saved'

    I guess the os.path.sep.join is more robust and can be used w/o modifications for any os.

    0 讨论(0)
  • 2020-12-13 17:48

    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')
    
    0 讨论(0)
提交回复
热议问题