I'm not able to see the bigger picture here I think; but basically I have no idea why you would use os.path.join
instead of just normal string concatenation?
I have mainly used VBScript so I don't understand the point of this function.
I'm not able to see the bigger picture here I think; but basically I have no idea why you would use os.path.join
instead of just normal string concatenation?
I have mainly used VBScript so I don't understand the point of this function.
Write filepath manipulations once and it works across many different platforms, for free. The delimiting character is abstracted away, making your job easier.
You no longer need to worry if that directory path had a trailing slash or not. os.path.join
will add it if it needs to.
Using os.path.join
makes it obvious to other people reading your code that you are working with filepaths. People can quickly scan through the code and discover it's a filepath intrinsically. If you decide to construct it yourself, you will likely detract the reader from finding actual problems with your code: "Hmm, some string concats, a substitution. Is this a filepath or what? Gah! Why didn't he use os.path.join
?" :)
os.path.join
, but if you do, do it for the right reasons. There is a lot of cargo-culting around os.path.join. "A foolish consistency is the hobgoblin of little minds," as they say.
path + '/' + file
. This is imminently readable and portable to all OS. Using os.path.join
is hazardous because it is inconsistent. For example os.path.join('/foo/bar','test')
will return /foo/bar/test
on some systems and /foo/bar\\test
on others.
Mar 2, 2022 at 15:49
os.path.join('a', '/b')
, which actually returns /b
. So if you're mixing these methods, or converting from string concatenation to os.path.join
, make sure path components don't unexpectedly have a leading slash.
Will work on Windows with '\' and Unix (including Mac OS X) with '/'.
for posixpath here's the straightforward code
In [22]: os.path.join??
Type: function
String Form:<function join at 0x107c28ed8>
File: /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py
Definition: os.path.join(a, *p)
Source:
def join(a, *p):
"""Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded."""
path = a
for b in p:
if b.startswith('/'):
path = b
elif path == '' or path.endswith('/'):
path += b
else:
path += '/' + b
return path
don't have windows but the same should be there with '\'
/
works on Windows, with CPython…
Dec 19, 2012 at 1:51
It is OS-independent. If you hardcode your paths as C:\Whatever they will only work on Windows. If you hardcode them with the Unix standard "/" they will only work on Unix. os.path.join detects the operating system it is running under and joins the paths using the correct symbol.
If you hardcode them with the Unix standard "/" they will only work on Unix.
That is wrong. "/" works just fine on windows AND linux/unix/bsd/darwin.
Jul 2, 2016 at 11:18