Pathlib 正在接受一个 os.PathLike 对象,并将其解释为 bool
Pathlib is taking an os.PathLike object, and interpereting it as bool
问题
我目前正在做的项目有一个莫名其妙的错误,要么是我太笨了,要么就是太晦涩和技术性了。
我试图首先找到一个目录,返回它的路径,然后检查其中是否存在子目录。
from pathlib import Path
from os.path import isdir
from os import getenv
from subprocess import Popen
def find_dlDir():
example_dlHomeDirs = [
Path(getenv("HOME"), 'downloads'),
Path(getenv("HOME"), 'Downloads'),
]
if dlHome := getenv("DOWNLOADS_HOME") != None:
return dlHome
else:
for path in example_dlHomeDirs:
if isdir(path):
return path
return None
def dirExists(dirName: str):
if dlHome := find_dlHome() != None:
if isdir(Path(dlHome, dirName)):
return True
else:
return False
else:
print("No Downloads Folder found.\nTo resolve, create a new folder in \
your home folder with one of the following names:")
[print(name) for name in ['downloads', 'Downloads']]
exit(1)
def mkdir(path: Path, dirToMake: str):
"""
Make a directory with the name <dirToMake> at <path>
kwargs["path"]? Parent directory of <dirToMake>
kwargs["dirToMake"]? Name of the to-be-made directory
"""
Popen("mkdir", f"{str(path)}/{dirToMake}")
if __name__ == "__main__":
dir = "example"
if not dirExists(dirName=dir):
mkdir(path=getenv("DOWNLOADS_HOME"), dirToMake=dir)
下面的代码应该——使用下面的文件系统——运行:mkdir $HOME/Downloads/example
.
/Users/dickssau000 ¬
----Downloads ¬
--------github.com
--------youtube.com
--------mega.nz
相反,我得到了回溯:
Traceback (most recent call last):
File "/Users/dickssau000/.local/src/github.com/Saul-Dickson/dl/test.py", line 48, in <module>
if not dirExists(dirName=dir):
File "/Users/dickssau000/.local/src/github.com/Saul-Dickson/dl/test.py", line 24, in dirExists
if isdir(Path(dlHome, dirName)):
File "/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pathlib.py", line 1071, in __new__
self = cls._from_parts(args, init=False)
File "/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pathlib.py", line 696, in _from_parts
drv, root, parts = self._parse_args(args)
File "/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pathlib.py", line 680, in _parse_args
a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not bool
我绝对确定在第 24 行,变量 dlHome 和 dirName 分别是 os.PathLike 和 str。这些变量都不应该是 bool 类型,我完全不知道如何修复它。有人知道这里发生了什么吗?
python: 3.9
if dlHome := find_dlHome() != None:
...
这相当于
if dlHome := (find_dlHome() != None):
...
意思是dlHome
是bool
的类型,不是find_dlHome()
的结果!你想要这个:
if (dlHome := find_dlHome) is not None:
...
(您还应该使用 is
/is not
而不是 ==
/!=
进行 None
检查)
问题
我目前正在做的项目有一个莫名其妙的错误,要么是我太笨了,要么就是太晦涩和技术性了。
我试图首先找到一个目录,返回它的路径,然后检查其中是否存在子目录。
from pathlib import Path
from os.path import isdir
from os import getenv
from subprocess import Popen
def find_dlDir():
example_dlHomeDirs = [
Path(getenv("HOME"), 'downloads'),
Path(getenv("HOME"), 'Downloads'),
]
if dlHome := getenv("DOWNLOADS_HOME") != None:
return dlHome
else:
for path in example_dlHomeDirs:
if isdir(path):
return path
return None
def dirExists(dirName: str):
if dlHome := find_dlHome() != None:
if isdir(Path(dlHome, dirName)):
return True
else:
return False
else:
print("No Downloads Folder found.\nTo resolve, create a new folder in \
your home folder with one of the following names:")
[print(name) for name in ['downloads', 'Downloads']]
exit(1)
def mkdir(path: Path, dirToMake: str):
"""
Make a directory with the name <dirToMake> at <path>
kwargs["path"]? Parent directory of <dirToMake>
kwargs["dirToMake"]? Name of the to-be-made directory
"""
Popen("mkdir", f"{str(path)}/{dirToMake}")
if __name__ == "__main__":
dir = "example"
if not dirExists(dirName=dir):
mkdir(path=getenv("DOWNLOADS_HOME"), dirToMake=dir)
下面的代码应该——使用下面的文件系统——运行:mkdir $HOME/Downloads/example
.
/Users/dickssau000 ¬
----Downloads ¬
--------github.com
--------youtube.com
--------mega.nz
相反,我得到了回溯:
Traceback (most recent call last):
File "/Users/dickssau000/.local/src/github.com/Saul-Dickson/dl/test.py", line 48, in <module>
if not dirExists(dirName=dir):
File "/Users/dickssau000/.local/src/github.com/Saul-Dickson/dl/test.py", line 24, in dirExists
if isdir(Path(dlHome, dirName)):
File "/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pathlib.py", line 1071, in __new__
self = cls._from_parts(args, init=False)
File "/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pathlib.py", line 696, in _from_parts
drv, root, parts = self._parse_args(args)
File "/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pathlib.py", line 680, in _parse_args
a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not bool
我绝对确定在第 24 行,变量 dlHome 和 dirName 分别是 os.PathLike 和 str。这些变量都不应该是 bool 类型,我完全不知道如何修复它。有人知道这里发生了什么吗?
python: 3.9
if dlHome := find_dlHome() != None:
...
这相当于
if dlHome := (find_dlHome() != None):
...
意思是dlHome
是bool
的类型,不是find_dlHome()
的结果!你想要这个:
if (dlHome := find_dlHome) is not None:
...
(您还应该使用 is
/is not
而不是 ==
/!=
进行 None
检查)