Fix duplicate file bug

This commit is contained in:
2022-12-10 03:06:04 -05:00
parent 40c1174ff2
commit cd912319e9

View File

@@ -6,7 +6,7 @@
# Written by Jadon Yack (jyack) # Written by Jadon Yack (jyack)
from os import scandir, getlogin, rename from os import scandir, getlogin, rename
from os.path import splitext, exists, join from os.path import splitext, exists, join, isfile
from shutil import move as mv from shutil import move as mv
from time import sleep from time import sleep
@@ -22,23 +22,24 @@ vid_dst = f"/home/{user}/Videos"
doc_dst = f"/home/{user}/Documents" doc_dst = f"/home/{user}/Documents"
iso_dst = f"/home/{user}/ISOs" iso_dst = f"/home/{user}/ISOs"
def makeUniq(dst, name): def makeUniq(file, dst, name):
filename, extension = splitext(name) filename, extension = splitext(name)
counter = 1 counter = 1
uniq_name = name
# If the file exists, add the counter to the filename # If the file exists, add the counter to the filename
while exists(f"{dst}/{name}"): while exists(f"{dst}/{name}"):
name = f"{filename}({counter}){extension}" uniq_name = f"{filename}({counter}){extension}"
counter += 1 counter += 1
return name return uniq_name
def move(entry, name, dst_dir): def move(entry, name, dst_dir):
if exists(f"{dst_dir}/{name}"): # Generate a unique file name and move the file to the destination
uniq_name = makeUniq(dst_dir, name) # directory
old_name = join(dst_dir, name) uniq_name = makeUniq(entry, dst_dir, name)
new_name = join(dst_dir, uniq_name) # Check that entry is a file before moving it
rename(old_name, new_name) if (isfile(entry.path)):
mv(entry, dst_dir) mv(entry.path, f"{dst_dir}/{uniq_name}")
class FileHandler(FileSystemEventHandler): class FileHandler(FileSystemEventHandler):
def on_modified(self, event): def on_modified(self, event):