Behaviour of os.path.join

MRAB python at mrabarnett.plus.com
Thu May 28 18:33:20 EDT 2020


On 2020-05-28 22:34, Terry Reedy wrote:
> On 5/28/2020 9:19 AM, Eryk Sun wrote:
>> On 5/26/20, BlindAnagram <blindanagram at nowhere.com> wrote:
>>>
>>> But if I try to make the directory myself (as I tried first):
>>>
>>>    join(base, '..\\..\\', 'build', '\\')
>>>
>>> I obtain:
>>>
>>> 'C:\\'
>>>
>>> The documentation says that an absolute path in the parameter list for
>>> join will discard all previous parameters but '\\' is not an absoute path!
>> 
>> First, to be clear, a rooted path in Windows is not absolute (*), but
>> that's not relevant here. What happens is that joining a path is like
>> issuing successive "cd" commands in the shell, and "cd \" takes you to
>> the root path of the current drive. The implementation of
>> ntpath.join() similarly tracks the current drive via
>> ntpath.splitdrive() while joining components.
> 
> Ah.  cd only changes the cwd within a device. Changing the active device
> is done otherwise.  In Command Prompt
> 
> C:\Users\Terry>f: # : required
> 
> F:\>c:
> 
> C:\Users\Terry>cd f:
> F:\
> 
> C:\Users\Terry>cd f:/dev
> 
> C:\Users\Terry>f:
> 
> f:\dev>
> 
cd has a switch /d that will also change the drive:

C:\Users\Terry>cd /d f:/dev

f:\dev>


More information about the Python-list mailing list