小编Acc*_*ate的帖子

为什么即使路径不存在,Path(...).exists 也为 true?

我应该要求用户输入一个目录,如果该目录不存在,我们告诉他们,然后为他们创建一个目录。

这是我到目前为止的代码。无论输入的路径的目录是否实际存在,它都会以相同的方式进行操作。在 Windows 上使用 pathlib 时会发生这种情况。错误报告 Issue35692

import pathlib
from pathlib import Path

def directory():

    p = Path(input("Enter file path: "))

    if p.exists:

        print('Exists')

        return p

    else:

        print('Directory does not exist. Making directory for you.')

        p.mkdir()



directory()
Run Code Online (Sandbox Code Playgroud)

python windows directory pathlib

5
推荐指数
1
解决办法
5204
查看次数

标签 统计

directory ×1

pathlib ×1

python ×1

windows ×1