我有以下字典:
config = {
'base_dir': Path(__file__).resolve(strict=True).parent.absolute(),
'app_dir': '<base_dir>/app'
}
我想替换<base_dir>为的值,<base_dir>
所以我这样做了:
for key, value in config.items():
if type(value) == str:
vars_to_replace = re.findall(r'\<.+\>',value)
for var in vars_to_replace:
config[key] = value.replace(var,config[var[1:-1]])
目前收到错误:TypeError: replace() argument 2 must be str, not WindowsPath
如果我config[var[1:-1]]用str(). 它消除了错误,但我丢失了 WindowsPath,现在 app_dir 变成了一个字符串。我想将对象保留为 WindowsPath。