You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2019-03-18.04:08:02.973>created_at=<Date2019-03-18.02:12:20.452>labels= ['invalid', 'type-bug', '3.7', 'OS-windows']
title='pathlib.Path unexpected (wrong) behaviour when combining paths in a for loop'updated_at=<Date2019-03-18.04:08:02.971>user='https://bugs.python.org/MikeDavies'
This is true whether I use for loops or list comprehensions.
To get the expected output I need to change the Paths to strings, combine them, then convert them back to paths like this:
########### INPUT:
print( [Path(str(a)+str(b)) for a in pa for b in pb] )
########### OUTPUT:
[WindowsPath('C:/Program Files/one/two/three.exe'), WindowsPath('C:/Program Files/four.exe'), WindowsPath('C:/one/two/three.exe'), WindowsPath('C:/four.exe')]
Interestingly if I print only 'a' I get the expected answer:
########### INPUT:
print( [a for a in pa for b in pb] )
########### OUTPUT:
[WindowsPath('C:/Program Files'), WindowsPath('C:/Program Files'), WindowsPath('C:/'), WindowsPath('C:/')]
And the same is true if I print only 'b':
########### INPUT:
print( [b for a in pa for b in pb] )
########### OUTPUT:
[WindowsPath('/one/two/three.exe'), WindowsPath('/four.exe'), WindowsPath('/one/two/three.exe'), WindowsPath('/four.exe')]
Additionally in some cases it does give the correct answer. Here is a similar example where the answer is correct:
########### INPUT:
pa = [Path('C:/'), Path('D:/')]
pb = [Path('a.exe'), Path('b.exe')]
print( [a/b for a in pa for b in pb] )
########### OUTPUT:
[WindowsPath('C:/a.exe'), WindowsPath('C:/b.exe'), WindowsPath('D:/a.exe'), WindowsPath('D:/b.exe')]
The drive is retained when a rooted path is joined to a drive-absolute or UNC-absolute path. This is documented behavior 1:
When several absolute paths are given, the last is taken as an
anchor (mimicking os.path.join()’s behaviour):
>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')
>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
However, in a Windows path, changing the local root doesn’t
discard the previous drive setting:
>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
If you want to simply append directories to a path, use a relative path. For example:
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: