which one should I use: os.sep or os.path.sep?
Question:
They are same, but which one should I use?
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.
Answers:
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.
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')
If you are using Python 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.
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.
- As mentioned in the python docs, both
os.path.sep
and os.sep
return the same output.
The character used by the operating system to separate pathname
components. This is ‘/’ for POSIX and ” for Windows.
-
Both of them belongs to the same python class also.
print(type(os.sep))
print(type(os.path.sep))
# Output
<class 'str'>
<class 'str'>
-
Both have them have the same documentation.
print(os.path.sep.__doc__)
print(os.sep.__doc__)
# The outputs of both print statements are the same.
So, I think after Python2 where we mostly used os.sep
, in Python3 only the consistency matters as far as their uses are concerned.
They are same, but which one should I use?
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 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.
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')
If you are using Python 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.
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.
- As mentioned in the python docs, both
os.path.sep
andos.sep
return the same output.
The character used by the operating system to separate pathname
components. This is ‘/’ for POSIX and ” for Windows.
-
Both of them belongs to the same python class also.
print(type(os.sep)) print(type(os.path.sep)) # Output <class 'str'> <class 'str'>
-
Both have them have the same documentation.
print(os.path.sep.__doc__) print(os.sep.__doc__) # The outputs of both print statements are the same.
So, I think after Python2 where we mostly used os.sep
, in Python3 only the consistency matters as far as their uses are concerned.