os.path.join(*list) not working as expected when trying to convert a list to a path

29 Views Asked by At

I'm trying to convert this list: a_list = ['c:', 'project_files', 'ProjA', 'B_Files']

into this path: 'c:\\project_files\\ProjA\\B_Files'

I'm using this:

a_list = ['c:', 'project_files', 'ProjA', 'B_Files']
my_path = os.path.join(*a_list)

However this is what I get: 'c:project_files\\ProjA\\B_Files'

Why isn't there a \\ after c: ? I was reading some similar questions and apparently it has something to do with this not being an absolute path but a relative one, but I'm still unsure how to get what I want

I'd appreciate any advice

1

There are 1 best solutions below

2
BTables On

This is covered directly on the official documentation. Relevant section:

Note that 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.