Python copy files to a new directory and rename if file name already exists

后端 未结 4 689
梦毁少年i
梦毁少年i 2021-02-02 06:24

I\'ve already read this thread but when I implement it into my code it only works for a few iterations.

I\'m using python to iterate through a directory (lets call it m

4条回答
  •  误落风尘
    2021-02-02 07:05

    I would say you have an indentation problem, at least as you wrote it here:

    while not os.path.exists(file + "_" + str(i) + extension):
       i+=1
       print "Already 2x exists..."
       print "Renaming"
       shutil.copy(path, file + "_" + str(i) + extension)
    

    should be:

    while os.path.exists(file + "_" + str(i) + extension):
        i+=1
    print "Already 2x exists..."
    print "Renaming"
    shutil.copy(path, file + "_" + str(i) + extension)
    

    Check this out, please!

提交回复
热议问题