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

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

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

解决方法:code

(1)加入list用append添加。ci

(2)相似这样的写法:
"/".join([root_path,file_name]) 将须要加入的东西先链接起来,而后用[ ]组合.
it

举个例子:class

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'
相关文章
相关标签/搜索