如何克服Python 3.4 NameError:名称'basestring'未定义

2024-01-26

我在本地目录中有一个名为 hello.txt 的文件以及 test.py,其中包含以下 Python 3.4 代码:

import easywebdav
webdav = easywebdav.connect('192.168.1.6', username='myUser', password='myPasswd', protocol='http', port=80)
srcDir = "myDir"
webdav.mkdir(srcDir)
webdav.upload("hello.txt", srcDir)

当我运行这个时,我得到这个:

Traceback (most recent call last):
  File "./test.py", line 196, in <module>
    webdav.upload("hello.txt", srcDir)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/easywebdav/client.py", line 153, in upload
    if isinstance(local_path_or_fileobj, basestring):
NameError: name 'basestring' is not defined

谷歌搜索这个结果有几个点击,所有这些point https://github.com/oxplot/fysom/issues/1进行相同的修复,以防将来路径移动,包括“在导入类型之后”:

try:
    unicode = unicode
except NameError:
    # 'unicode' is undefined, must be Python 3
    str = str
    unicode = str
    bytes = bytes
    basestring = (str,bytes)
else:
    # 'unicode' exists, must be Python 2
    str = str
    unicode = unicode
    bytes = str
    basestring = basestring

我没有使用导入类型,但包含或不包含它似乎在 PyDev 中没有什么区别 - 无论哪种方式我都会收到错误。导致错误的行是:

unicode = unicode

说“未定义的变量”。

好吧,我的 python 知识在这一点上动摇了,我在这个网站上寻找过类似的帖子,但没有找到一个足够具体的基础字符串,我理解可以提供帮助。我知道我需要指定基本字符串,但我不知道如何做。有人会足够仁慈地为我指明正确的方向吗?


您可以更改 easywebdav 的 client.py 文件,就像此签入中的前两个更改一样:https://github.com/hhaderer/easywebdav/commit/983ced508751788434c97b43586a68101eaee67b https://github.com/hhaderer/easywebdav/commit/983ced508751788434c97b43586a68101eaee67b

变化包括替换basestring by str在 client.py 中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何克服Python 3.4 NameError:名称'basestring'未定义 的相关文章

随机推荐