Difference between os.path.isdir and Path.is_dir

Cameron Simpson cs at cskk.id.au
Thu Jul 25 19:25:37 EDT 2019


On 26Jul2019 03:43, Chris Angelico <rosuav at gmail.com> wrote:
>On Fri, Jul 26, 2019 at 3:28 AM eryk sun <eryksun at gmail.com> wrote:
>> On 7/25/19, Kirill Balunov <kirillbalunov at gmail.com> wrote:
>> >>>> import os
>> >>>> from pathlib import Path
>> >>>> dummy = " "   # or "" or "     "
>> >>>> os.path.isdir(dummy)
>> > False
>> >>>> Path(dummy).is_dir()
>> > True
>>
>> I can't reproduce the above result in either Linux or Windows. The
>> results should only be different for an empty path string, since
>> Path('') is the same as Path('.'). The results should be the same for
>> Path(" "), depending on whether a directory named " " exists (normally
>> not allowed in Windows, but Linux allows it).
>
>Try an empty string, no spaces. To pathlib.Path, that means the
>current directory. To os.path.abspath, that means the current
>directory. To os.stat, it doesn't exist.

And for some context, on older UNIXen "" did stat successfully.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list