0

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.

2
  • 1
    file == os.path.isfile(os.getcwd() + "\\" + file) is never true, because file is never True or False, but os.path.isfile() only ever returns a boolean. Commented Aug 22, 2019 at 16:05
  • 1
    Next, even if the if tests would ever be True, you used break and so cut the loop short. The remainder of filenames in dircontents is never processed. Commented Aug 22, 2019 at 16:06

1 Answer 1

1

This is incorrect:

if file == os.path.isfile(os.getcwd() + "\\" + file):

isfile() returns True or False, not a filename. You should just test the result of the call, not compare it with the filename.

There's also no need to prepend os.getcwd() -- relative pathnames are always interpreted relative to the current directory. Similarly, os.listdir() defaults to the current directory.

So it should be:

if os.path.isfile(file):

And you shouldn't have break -- that makes the loop stop after the first thing it removes.

And use elif for the second test, since the two cases are mutually exclusive.

dircontents = os.listdir()
for file in dircontents:
    if os.path.isfile(file) and file not in ['testfile.py', 'logo.ico', 'settings.xml', 'settings_backup.xml']:
        os.remove(file)
    elif os.path.isdir(file) and file != 'temp':
        os.rmdir(file)

newfiles = os.listdir("temp")
for f in newfiles:
    shutil.move("temp\\" + f, os.getcwd())
Sign up to request clarification or add additional context in comments.

5 Comments

Well, it quite works... I changed this part: elif os.path.isdir(file): if file != 'temp': os.rmdir(file) break to this because the directory I want to delete is not empty: elif os.path.isdir(file): if file != 'temp': shutil.rmtree(file, ignore_errors=True) break Although, It doesn't delete everything and I don't even get any errors.
Any clue how to do it?
break still ends the loop, so you won't remove anything after removing the directory.
That's why you're not removing everything.
Thank you. I totally forgot about it. It works now perfectly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.