os.path.join

Ethan Furman ethan at stoneleaf.us
Mon Oct 19 18:41:59 EDT 2009


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.



More information about the Python-list mailing list