简体   繁体   中英

Python doesn't delete every file/dir

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.

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM