python类型错误:can only concatenate list (not "str") to list

Posted 北向。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python类型错误:can only concatenate list (not "str") to list相关的知识,希望对你有一定的参考价值。

TypeError:can only concatenate list (not "str") to list:

类型错误:只能将list类型和list类型联系起来,而不是str类型;

解决方法:

(1)加入list用append添加。

(2)类似这样的写法:
"/".join([root_path,file_name]) 将需要加入的东西先连接起来,然后用[ ]组合.

举个例子:

project_path = ‘Exercise‘
current_path = os.path.dirname(os.path.abspath(project_path))  # 返回当前目录
path1 = current_path.split(project_path)
path2 = [path1[0],project_path]
log_path = path2 + current_time + ‘.log‘
这样的代码是会抛出异常TypeError:can only concatenate list (not "str") to list
修改后的代码:
project_path = ‘Exercise‘
current_path = os.path.dirname(os.path.abspath(project_path)) # 返回当前目录
path1 = current_path.split(project_path)
path2 = [path1[0],project_path]
path3 = ""
log_name = path3.join(path2) + ‘/logs/‘ # 将需要加入的东西先连接起来
log_path = log_name + current_time + ‘.log‘
 

以上是关于python类型错误:can only concatenate list (not "str") to list的主要内容,如果未能解决你的问题,请参考以下文章

成功解决TypeError: only size-1 arrays can be converted to Python scalars

python报错:only integer scalar arrays can be converted to a scalar index

only size-1 arrays can be converted to Python scalars

如何解决此滚动视图错误“ScrollView can host only one direct child”

关闭应用程序时多次出现错误“QObject::startTimer: QTimer can only be used with threads started with QThread”

andriod 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用(代码