0

Here is the code I am working on:

from qgis.core import*
import glob, os, shutil, time, qgis

path = r"C:\Temp\testinput"
dest = r"C:\Temp\testoutput"

fname = []
for root,d_names,f_names in os.walk(path):
    for f in f_names:
        if f.endswith('.kml'):
            src = os.path.join(root,f)
            print(time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(src))))
            print(os.path.realpath(src))
            shutil.copy2(src, dest)

this code transverses through the directory and copies the files but does overwrites files with the same name. How do I prevent the overwriting part? I would like to have the same type of filed renamed to "filename-copy" if it catches a file with the same name in the new folder.

Apmartin
  • 75
  • 2
  • 10

1 Answers1

0

Very quick answer;

If the extension is known, it is not that hard. You can check upfront if a file exists. By adding something like this

exists = os.path.isfile(dest)
if exists:
    os.rename(dest , dest.replace('.kml', '-copy.kml'))

So the entire thing looks like this:

from qgis.core import*
import glob, os, shutil, time, qgis

path = r"C:\Temp\testinput"
dest = r"C:\Temp\testoutput"

fname = []
for root,d_names,f_names in os.walk(path):
    for f in f_names:
        if f.endswith('.kml'):
            src = os.path.join(root,f)
            print(time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(src))))
            print(os.path.realpath(src))
            exists = os.path.isfile(dest)
            if exists:
                os.rename(dest , dest.replace('.kml', '-copy.kml'))
            shutil.copy2(src, dest)

Not so very quick answer;

But, this assumes that the "file-copy.kml" does not already exist. If you want to keep an X amount of copies, perhaps rename them differently. In that case, I would advice something like this:

    exists = os.path.isfile(dest)
    if exists:
        os.rename(dest , dest.replace('.kml', '-copy_'+id_generator()+'.kml'))

Where you place this someplace at the top of your file.

import string
import random
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

Where I have borrowed the function that generates some random characters from this question; Random string generation with upper case letters and digits in Python

Paddy
  • 123
  • 7
  • I keep getting a "syntaxerror: invalid syntax" at the end of the last line when I add in the new code. It appears it does not want to execute the shutil.copy2. How would I go about fixing this? This error did not appear before adding the code you recommended – Apmartin Jan 09 '19 at 15:18
  • My apologies, in the "os.rename" statement I forgot an extra ")". I've updated the post. – Paddy Jan 09 '19 at 15:32
  • Thank you I should have realized that. One more thing. It seems like the if statement with "isfile()" is not working, I read something that it may not be the absolute path. This code should be able to scan through an entire drive and then paste all ".kml"'s it into a singular folder on a local drive folder or a desktop folder. Do I have to change that up a bit to satisfy that if statement? – Apmartin Jan 09 '19 at 16:20
  • Ahh, now I see... On a bigger monitor in the evening. If variable "f" is the actual filename, and "dest" only the destination folder... Then it should be this (concat the folder and filename) because the copy command usually needs the entire location. shutil.copy2(src, os.path.join(dest, f)) – Paddy Jan 09 '19 at 17:36