os.path.join

Ethan Furman ethan at stoneleaf.us
Mon Oct 19 18:51:44 EDT 2009


Ethan Furman wrote:
> Following closely on the heels of the whole sum()ing strings debate, I 
> think I found an error -- at least, it's not documented to behave this 
> way...
> 
> def uncompress_job(job_num,
>                    save_path='z:\\old_jobs',
>                    restore_path='z:\\orders'):
>     destination = os.path.join([restore_path, job_num])
>     print os.path.join([save_path, job_num])
> 
> As the astute reader will doubt notice, job_num should be a string.  As 
> I was not an astute typer during development, I just put in an integer:
> 
> -->uncompress_job(18273)
> ['z:\\oldjobs', 19858]
> 
> I did have more interesting errors to lead me to this point, but here I 
> am, and my question... shouldn't os.path.join raise an exception if an 
> incompatible type is passed to it?  I sure wish it had!  ;-)
> 
> I was expecting, and the documentation led me to believe, that a string 
> would be returned, not a list.
> 
> ~Ethan~
> 
> 
> -- Documentation --
> join( path1[, path2[, ...]])
> 
> Join one or more path components intelligently. 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. The return value is the concatenation of path1, and 
> optionally path2, etc., with exactly one directory separator (os.sep) 
> inserted between components, unless path2 is empty. Note that on 
> Windows, since there is a current directory for each drive, 
> os.path.join("c:", "foo") represents a path relative to the current 
> directory on drive C: (c:foo), not c:\\foo.

*AHhhhhhh*

I am _so_ embarrassed.  Looks like I was supposed to pass individual 
items, not a single list.

Going to the code, I found this:

ntpath.py
---------
def join(a, *p):
     """Join two or more pathname components, inserting "\\" as needed"""
     path = a
     for b in p:
         .
         .
         .
     return path

and because there was nothing for *p, I just got back what I gave it. 
GIGO is alive and well!  ;-)

~Ethan~



More information about the Python-list mailing list