Python错误:TypeError:只能将列表(不是“str”)连接到列表

时间:2013-06-17 19:35:44

标签: python-2.7

我目前正在处理备份程序,在尝试使用给定目标重新生成唯一文件名时遇到了错误。我在我的代码中将此函数称为:getFileUnique(f,pathtofile(backup +“/”+“../ trash /”))。 f是文件路径,其余变量非常简单。

def getFileUnique(path,destination):
    path = path.replace("\\","/")
    p = path.split("/")[-1]
    if not os.path.exists(join(destination,p)):
        return destination+p
    j = p.split(".")
    counter = 0
    print(j)
    while os.path.exists(join(destination,j[:-1]+str(counter)+"."+j[-1])):
        print(counter)
        print("asdfsdf")
        counter += 1
    return destination+j[:-1]+str(counter)+"."+j[-1]

错误:

Traceback (most recent call last):
  File "C:\Users\Owner\Google Drive\Programs\Dev Enviroment\python\backup\backup.py", line 76, in <module>
    main("files","backup")
  File "C:\Users\Owner\Google Drive\Programs\Dev Enviroment\python\backup\backup.py", line 73, in main
    updateBackup(oldf,newf,reg,backup)
  File "C:\Users\Owner\Google Drive\Programs\Dev Enviroment\python\backup\backup.py", line 65, in updateBackup
    k = getFileUnique(f,pathtofile(backup+"/"+"../trash/"))
  File "C:\Users\Owner\Google Drive\Programs\Dev Enviroment\python\backup\backup.py", line 41, in getFileUnique
    while os.path.exists(join(destination,j[:-1]+str(counter)+"."+j[-1])):
TypeError: can only concatenate list (not "str") to list

1 个答案:

答案 0 :(得分:0)

return destination + '.'.join(j[:-1]) + str(counter) + "." + j[-1]