Python异常之AttributeError: 'tuple' object has no attribute 'startswith'

AttributeError: 'tuple' object has no attribute 'startswith'

中文直译属性错误,元组对象没有这个属性,一个方法

原因:ntpath.py模块下的 join函数,第一个参数是位置参数,第二个是元组参数(也称无名参数)

我在调用时,直接传进去一个turple对象,我应该是去传一个 *tempTurple ,代表解包传入的元组参数

等同于

join(xxx, tupleElement1, tupleElement2)

print os.path.join(os.path.split(__file__)[0], tempTuple) #我的错误代码
def join(a, *p):
    """Join two or more pathname components, inserting '/' as needed.
    If any component is an absolute path, all previous path components
    will be discarded.  An empty last part will result in a path that
    ends with a separator."""
    path = a
    for b in p:
        if b.startswith('/'): # tuple是没有startswith方法的,只有str有嘛
            path = b
        elif path == '' or path.endswith('/'):
            path +=  b
        else:
            path += '/' + b
    return path

 

你可能感兴趣的:(Python,综合)