首页 文章

Python 3.5,ctypes:TypeError:期望的字节或整数地址而不是str实例

提问于
浏览
7

我遇到了ctypes的问题 . 我认为我的类型转换是正确的,错误对我没有意义 . “arg - ct.c_char_p(logfilepath)”行错误TypeError:预期的字节或整数地址而不是str实例

我试过python 3.5和3.4 .

功能我打电话:

stream_initialize('stream_log.txt')

Stream_initialize代码“

def stream_initialize(logfilepath):
    f = shim.stream_initialize
    arg = ct.c_char_p(logfilepath)
    result = f(arg)

    if result:
        print(find_shim_error(result))

2 回答

  • 11

    c_char_p 需要 bytes 对象,因此您必须先将 string 转换为 bytes

    ct.c_char_p(logfilepath.encode('utf-8'))
    

    另一种解决方案是使用 c_wchar_p 类型,它采用 string .

  • 1

    为了完整起见:也可以将其称为 stream_initialize(b'stream_log.txt') . 注意字符串前面的 b ,这会导致它被解释为 bytes 对象 .

相关问题