Behaviour of os.path.join

BlindAnagram blindanagram at nowhere.com
Wed May 27 17:00:13 EDT 2020


On 27/05/2020 18:37, Roel Schroeven wrote:

> BlindAnagram schreef op 27/05/2020 om 18:39:
>> On 27/05/2020 16:49, Rhodri James wrote:
>>> On 27/05/2020 16:12, BlindAnagram wrote:
>>>> I'm sorry that you don't believe me but all I know is how I intend the
>>>> path to be used.  And the os.path functions aren't helpful here when
>>>> they actually_change_  the meanings of paths on Windows:
>>>>
>>>>>> fp= "C:\\Documents\finance\\"
>>>>>> abspath(fp)
>>>> 'C:\\Documents\\finance'
>>>>
>>>> If you believe these 'before' and 'after' paths are the same I can only
>>>> assume that you don't work on Windows (where one refers to a directory
>>>> and the other a file without an extension).
>>>
>>> More accurately, one is not a legal filename but both are legal
>>> directory names.
>>
>> If they are to be created, which is my situation, the result will be a
>> diretory and a file.
> 
> os.mkdir('C:\\Documents\\finance') creates a directory.
> open('C:\\Documents\\finance', 'w') creates a file.
> 
> The difference is in the operation, not in the name.
> 
> 'C:\\Documents\\finance' is a pathname, which can refer to either a
> directory or a file.
> 'C:\\Documents\\finance\\' could refer to a directory, but to me looks
> more like a partial pathname, not a complete one.
> 
> I can't think of any reason for ending pathnames with (back)slashes.
> Just use os.path.join(directory, filename) when you need to refer to a
> file in the directory.
> 
>> I would be surprised if issues such as these were not, at least in
>> significant part, the reason why we now have pathlib.
> 
> That should be easy to verify: the reasons are listed in the PEP:
> https://www.python.org/dev/peps/pep-0428
> I don't see your issue there. I don't think anyone has ever considered
> it an issue at all really.
> 
>>> How are these unexpected extensionless files getting created?
>>
>> I believe by attempting to make the directory I send absolute with
>> abspath() and then copying a file to this path.  They expected this to
>> copy the file into the directory with its original name but instead it
>> copies it to the file that abspath 'kindly' converts my directory into.
> 
> We're getting closer to the real issue here. What functions were used
> copy these files? With which parameters?
> Were the destination directories created before copying the files to them?
> 
>> I did complain about their lack of knowledge but I also have a right to
>> complain about a function that converts an explicitly specified
>> directory into a file :-)
> 
> Again, a pathname is never inherently a directory or a file.

You can define a path however you want but it won't change the fact that
on Windows a path that ends in '\\' is inherently a path to a directory.


More information about the Python-list mailing list