This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.path.join when second argument starts with '/' (linux/unix)
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7, Python 2.6, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: al_form2000, amaury.forgeotdarc, daniel.urban, eric.smith
Priority: normal Keywords:

Created on 2011-03-02 17:45 by al_form2000, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg129906 - (view) Author: Alessandro Forghieri (al_form2000) Date: 2011-03-02 17:45
Observed behavior:

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

Expected behavior:

should return '/foo/bar/baz' or (suboptimally) raise an exception.

Why:
Observed beahvior violates the minimal surprise principle and does not appear to respond to any particular rationale (by the same token it could return /foo/bar as well)
msg129908 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2011-03-02 17:56
It does exactly what is documented: "If any component is an absolute path, all previous components (on Windows, including the previous drive letter, if there was one) are thrown away, and joining continues." (http://docs.python.org/dev/py3k/library/os.path#os.path.join)
msg129909 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-03-02 18:04
And the current behaviour is useful; for example, the absolute path of a file name can be computed with os.path.join(os.getcwd(), filename).
This works with both absolute and relative paths.

os.path.join() won't change; you probably should consider another function, like os.path.normpath(a + '/' + b)
msg129911 - (view) Author: Alessandro Forghieri (al_form2000) Date: 2011-03-02 18:15
Documented it is, and change it may not, but I am still unconvinced.

Anything that joins "should" (IMHO) preserve the leading stuff - this one throws it away - sometimes. Also I do believe a number of other languages take a different stance (I am no pitonista, but took a double take when I noticed this).
msg129915 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2011-03-02 19:35
I appreciate that you're unconvinced of its usefulness, but you can be assured that some of us do find it a useful and desirable behavior. And Python isn't the only system that works this way, emacs does the same thing when you enter filenames.
History
Date User Action Args
2022-04-11 14:57:13adminsetgithub: 55587
2011-03-02 19:35:35eric.smithsetnosy: + eric.smith
messages: + msg129915
2011-03-02 18:15:26al_form2000setnosy: amaury.forgeotdarc, daniel.urban, al_form2000
messages: + msg129911
2011-03-02 18:04:55amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg129909

resolution: not a bug
stage: resolved
2011-03-02 17:56:50daniel.urbansetnosy: + daniel.urban
messages: + msg129908
2011-03-02 17:49:00al_form2000settitle: os.path.join when second starts with '/' (linux/unix) -> os.path.join when second argument starts with '/' (linux/unix)
2011-03-02 17:45:03al_form2000create