Python TypeError:强制使用Unicode:需要字符串或缓冲区,找到属性
我得到一个错误:“TypeError:强制使用Unicode:需要字符串或缓冲区,找到属性”。我不确定这种通过酒店提供信息的方式有什么问题 MainApp.py的相关代码Python TypeError:强制使用Unicode:需要字符串或缓冲区,找到属性,python,python-2.7,Python,Python 2.7,我得到一个错误:“TypeError:强制使用Unicode:需要字符串或缓冲区,找到属性”。我不确定这种通过酒店提供信息的方式有什么问题 MainApp.py的相关代码 from constantsconfig import _Const if not os.path.exists(_Const.CONFIG_DIRECTORY): //error happens on this line os.makedirs(_Const.CONFIG_DIRECTORY) ConstantC
from constantsconfig import _Const
if not os.path.exists(_Const.CONFIG_DIRECTORY): //error happens on this line
os.makedirs(_Const.CONFIG_DIRECTORY)
ConstantConfig.py的相关代码
import os
def constant(f):
def fset(self, value):
raise TypeError
def fget(self):
return f()
return property(fget, fset)
class _Const(object):
@constant
def MAIN_CRON_JOB_ID(self):
return "MAIN_CRON_JOB_ID"
@constant
def DOWNLOAD_PHOTOS_INTERVAL_IN_MINUTES(self):
return 15
@constant
def BASE_API_URL(self):
return "http://example.com/file.php"
@constant
def API_KEY_RASPBERRIES(self):
return "fasd79f97fsfa80f809f809fasd890fsda76fds5f54ds465fdas456"
错误消息
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.1\helpers\pydev\pydevd.py", line 1664, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2018.1\helpers\pydev\pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2018.1\helpers\pydev\pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:/_Data_/MainApp/RaspberriPi/_App/mainapp.py", line 192, in <module>
if not os.path.exists(_Const.CONFIG_DIRECTORY):
File "C:\_Data_\MainApp\RaspberriPi\_App\venv\lib\genericpath.py", line 26, in exists
os.stat(path)
TypeError: coercing to Unicode: need string or buffer, property found
回溯(最近一次呼叫最后一次):
文件“C:\Program Files\JetBrains\PyCharm 2018.1\helpers\pydev\pydevd.py”,第1664行,在
main()
文件“C:\Program Files\JetBrains\PyCharm 2018.1\helpers\pydev\pydevd.py”,第1658行,主目录
globals=debugger.run(setup['file'],None,None,is_模块)
文件“C:\Program Files\JetBrains\PyCharm 2018.1\helpers\pydev\pydevd.py”,第1068行,正在运行
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“C://\u Data\uuu/MainApp/RaspberriPi/\u App/MainApp.py”,第192行,在
如果不存在os.path.exists(_Const.CONFIG_目录):
文件“C:\\u Data\uuu\MainApp\RaspberriPi\\u App\venv\lib\genericpath.py”,第26行,存在于
os.stat(路径)
TypeError:强制使用Unicode:需要字符串或缓冲区,找到属性
您正在使用类\u Const
。这是错误的,您应该使用类\u Const
的实例
比如说,
const = _Const()
if not os.path.exists(const.CONFIG_DIRECTORY):
os.makedirs(const.CONFIG_DIRECTORY)
或者更好
const = _Const()
try:
os.mkdir(const.CONFIG_DIRECTORY)
except FileExistsError:
pass
试试
\u Const().CONFIG\u DIRECTORY
.mmm,这就成功了,我(还)不太熟悉python,一旦你知道了就这么简单了