From 3751666d2e0dde2c8d89d200f2fcbc65bc4bbe15 Mon Sep 17 00:00:00 2001 From: pythonboi Date: Fri, 1 Jul 2022 07:37:38 -0400 Subject: [PATCH 1/2] fix the bug for a file already exists in a exiting directory --- file_placer.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/file_placer.py b/file_placer.py index 40332c1..463b191 100644 --- a/file_placer.py +++ b/file_placer.py @@ -135,12 +135,26 @@ # # print(roots) for fileCheck in lstDir: if os.path.isfile(fileCheck): - getFileName = fileCheck + getFileName += fileCheck print(f"{getFileName} this is for the loop") for name in lstDir: dirPath = os.path.join(dirCheck, name) - print(dirPath) + for roots, dirs, files in os.walk(dirPath): + if getFileName in files: + theDir = os.getcwd() + print(lstDir) + print(theDir) + os.remove(getFileName) + print(f"{getFileName} already exits" + # getFileName# " + ) + + # print(files) + # print(dirPath) + # + # if getFileName in dirPath: + # for data in dirPath: From 91d9aa56d89e97b6be82e515f631e87f8f4447f9 Mon Sep 17 00:00:00 2001 From: pythonboi Date: Sat, 2 Jul 2022 08:15:57 -0400 Subject: [PATCH 2/2] clean up the code and update the direcotry path where the file is verify from --- file_placer.py | 58 +++++++++++--------------------------------------- 1 file changed, 12 insertions(+), 46 deletions(-) diff --git a/file_placer.py b/file_placer.py index 463b191..3fb9c4c 100644 --- a/file_placer.py +++ b/file_placer.py @@ -13,7 +13,6 @@ newfileExt = [] newSet = set() fullPath = '' -# getExistingDir = '' if currentDirectory != chgeDirectory: os.chdir(chgeDirectory) @@ -79,17 +78,15 @@ os.mkdir(uniqFile) # .title()) print(f"{uniqFile} created successfully") except FileExistsError: - print(f"{uniqFile} already exists ") + print(f"{uniqFile} folder already exists ") for vryFile in lstDir: - # print(vryFile) if os.path.isfile(vryFile): fullPath = os.path.join(chgeDirectory, vryFile) - # print(f"{fullPath}, read this first") for chkFolder in folderSide: - # print(chkFolder) + getExistingDir = os.path.join(chgeDirectory, chkFolder) try: @@ -99,64 +96,33 @@ if word in fullPath and word in getExistingDir: print(shutil.move(fullPath, getExistingDir)) - elif os.path.isfile(vryFile) and vryFile in getExistingDir: - print(f"{vryFile}, I am printing this") + # elif os.path.isfile(vryFile) and vryFile in getExistingDir: + # print(f"{vryFile}, I am printing this") - # elif vryFile in getExistingDir: - # print(f"{vryFile} already exists in directory") except shutil.Error: print(f"{vryFile} file already exits") -# A new code begins from here. Everything works well from here until adding new code to it +# A new code begins from here. The code above works well and adding new code below # Check if the file or folder already exits -# dirPath = os.path.join(chgeDirectory, lstDir) -# for read in os.listdir(): -# getdirfullpathName = os.path.join(chgeDirectory, read) -# print(getdirfullpathName) -# # if -# os.wal - -dirCheck = r'C:\Users\admin\Downloads' -# print(folderSide) +# dirCheck = r'C:\Users\admin\Downloads' -# for ck in fileSide: -# -# if os.path.isfile(ck): -# print(ck) -# if os.path.isfile(): - -# print(ck) +print(chgeDirectory) getFileName = '' -# for roots, dirs, files in os.walk(chgeDirectory): # ".", topdown=True): -# # print(roots) + for fileCheck in lstDir: if os.path.isfile(fileCheck): getFileName += fileCheck - print(f"{getFileName} this is for the loop") for name in lstDir: - dirPath = os.path.join(dirCheck, name) + dirPath = os.path.join(chgeDirectory, name) for roots, dirs, files in os.walk(dirPath): if getFileName in files: - theDir = os.getcwd() - print(lstDir) - print(theDir) - os.remove(getFileName) - print(f"{getFileName} already exits" - # getFileName# " - ) - - # print(files) - # print(dirPath) - # - # if getFileName in dirPath: - + print(f"{getFileName} already exits in ", os.path.abspath(dirPath)) + print(f"Removing {getFileName} in ", os.getcwd()) + os.remove(getFileName) - # for data in dirPath: - # os.listdir(data) - # if