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

后端 未结 4 629
梦毁少年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!

    0 讨论(0)
  • 2021-02-02 07:14

    I always use the time-stamp - so its not possible, that the file exists already:

    import os
    import shutil
    import datetime
    
    now = str(datetime.datetime.now())[:19]
    now = now.replace(":","_")
    
    src_dir="C:\\Users\\Asus\\Desktop\\Versand Verwaltung\\Versand.xlsx"
    dst_dir="C:\\Users\\Asus\\Desktop\\Versand Verwaltung\\Versand_"+str(now)+".xlsx"
    shutil.copy(src_dir,dst_dir)
    
    0 讨论(0)
  • 2021-02-02 07:20

    Sometimes it is just easier to start over... I apologize if there is any typo, I haven't had the time to test it thoroughly.

    movdir = r"C:\Scans"
    basedir = r"C:\Links"
    # Walk through all files in the directory that contains the files to copy
    for root, dirs, files in os.walk(movdir):
        for filename in files:
            # I use absolute path, case you want to move several dirs.
            old_name = os.path.join( os.path.abspath(root), filename )
    
            # Separate base from extension
            base, extension = os.path.splitext(filename)
    
            # Initial new name
            new_name = os.path.join(basedir, base, filename)
    
            # If folder basedir/base does not exist... You don't want to create it?
            if not os.path.exists(os.path.join(basedir, base)):
                print os.path.join(basedir,base), "not found" 
                continue    # Next filename
            elif not os.path.exists(new_name):  # folder exists, file does not
                shutil.copy(old_name, new_name)
            else:  # folder exists, file exists as well
                ii = 1
                while True:
                    new_name = os.path.join(basedir,base, base + "_" + str(ii) + extension)
                    if not os.path.exists(new_name):
                       shutil.copy(old_name, new_name)
                       print "Copied", old_name, "as", new_name
                       break 
                    ii += 1
    
    0 讨论(0)
  • 2021-02-02 07:20

    For me shutil.copy is the best:

    import shutil
    
    #make a copy of the invoice to work with
    src="invoice.pdf"
    dst="copied_invoice.pdf"
    shutil.copy(src,dst)
    

    You can change the path of the files as you want.

    0 讨论(0)
提交回复
热议问题