John
John

Reputation: 276

Python join directory paths

I have problems with os.path.join() because it never joins complete path. Code is:

get_base_dir = (os.getenv('BUILD_DIRECTORY'))
base_dir_path = pathlib.Path(get_base_dir)
print (base_dir_path ) # output is: F:\file\temp\ - which is correct

s_dir = '/sw/folder1/folder2/'
s_dir_path = pathlib.Path(s_dir)
print (s_dir_path) # output is: \sw\folder1\folder2\


full_path = os.path.join(base_dir_path, s_dir_path)

print (full_path)   # output is: F:\\sw\\folder1'\\folder2 instead of F:\\file\\temp\\sw\\folder1'\\folder2

Anyone has idea of what goes wrong?

Upvotes: 0

Views: 86

Answers (1)

Daweo
Daweo

Reputation: 36838

This behavior is compliant with what os.path.join docs states

Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

(I added emphasis)

Upvotes: 1

Related Questions