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'
相關文章
相關標籤/搜索