码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-02-16 15:38:40      阅读:3776      评论:0      收藏:0      [点我收藏+]

标签:解决方法   当前目录   nat   连接   lis   concat   python   current   project   

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

标签:解决方法   当前目录   nat   连接   lis   concat   python   current   project   

原文地址:https://www.cnblogs.com/yfacesclub/p/10387694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!