I want to move the contents of the "temp" folder to the main folder which contains let's say for an example 2 folders and 1 file:
1. Hello
2. World
3. python.exe
In the temp folder I have EXACT same content. I want to first delete the content of the main folder and then move the content from the temp folder to the main folder which should be empty because I deleted all the files and folders, right?
The problem is os.rmdir and os.remove doesn't delete all the files... It deletes maybe 2 files out of 4 and 1 folder out of 2. I'm not getting any permission related errors I only get the shutil error saying that the destination path already exists (Becauase it wasn't deleted by os.rmdir() for some reason).:
Traceback (most recent call last):
File "C:/Users/Test/Desktop/test_folder/ftpdl.py", line 346, in run
shutil.move(os.getcwd() + "\\temp\\" + f, os.getcwd())
File "C:\Python\lib\shutil.py", line 564, in move
raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path 'C:\Users\Test\Desktop\test_folder\Mono' already exists
My code looks like this:
dircontents = os.listdir(os.getcwd())
for file in dircontents:
if file == os.path.isfile(os.getcwd() + "\\" + file):
if file != 'testfile.py' and file != 'logo.ico' and file != 'settings.xml' and file != 'settings_backup.xml':
os.remove(os.getcwd() + "\\" + file)
break
if file == os.path.isdir(os.getcwd() + "\\" + file):
if file != 'temp':
os.rmdir(os.getcwd() + "\\" + file)
break
newfiles = os.listdir(os.getcwd() + "\\temp")
for f in newfiles:
shutil.move(os.getcwd() + "\\temp\\" + f, os.getcwd())
The expected result is that all of the old content of the main folder gets deleted and the new content from the temp folder gets moved to the main folder but It's quite not working. I would say It's partially working.
file == os.path.isfile(os.getcwd() + "\\" + file)is never true, becausefileis neverTrueorFalse, butos.path.isfile()only ever returns a boolean.iftests would ever beTrue, you usedbreakand so cut the loop short. The remainder of filenames indircontentsis never processed.