tags:

views:

735

answers:

6

Hello,

The below code will not join, when debugged the command does not store the whole path but just the last entry.

os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/')

When I test this it only stores the '/new_sandbox/' part of the code. Can anyone help.

Thanks

+12  A: 

The latter strings shouldn't start with a slash. If they start with a slash, then they're considered an "absolute path" and everything before them is discarded.

Quoting the Python docs for os.path.join:

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.

Craig McQueen
-1: **No** string should include a "/". One whole point of os.path.join is to prevent putting any slashes in the path.
S.Lott
@Craig, "full path" should be "absolute path".
Peter Hansen
@Peter: thanks, done.
Craig McQueen
+3  A: 

do not use forward slashes at the beginning of path components, except when refering to the root dir:

os.path.join('/home/build/test/sandboxes', todaystr, 'new_sandbox')

see also: http://docs.python.org/library/os.path.html#os.path.join

The MYYN
+1  A: 

Try with 'new_sandbox' only

os.path.join('/home/build/test/sandboxes/', todaystr, 'new_sandbox')
S.Mark
+5  A: 

It's because your '/new_sandbox/' begins with a / and thus is assumed to be relative to the root directory. Remove the leading /.

Amber
A: 

do it like this, without too the extra slashes

root="/home"
os.path.join(root,"build","test","sandboxes",todaystr,"new_sandbox")
ghostdog74
A: 

The idea of os.path.join is to make your program cross-platform (linux/win32/etc).

Even one slash ruins it.

So it only makes sense when being used with some kind of reference point like os.environ['HOME'] or os.path.dirname(__file__)

Antony Hatchkins