0

我目前正在开发一个备份程序,在尝试生成具有给定目标的唯一文件名时遇到了错误。我在我的代码中将此函数称为: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
4

1 回答 1

0
return destination + '.'.join(j[:-1]) + str(counter) + "." + j[-1]
于 2013-06-17T19:43:50.427 回答