Eugene
Eugene

Reputation: 161

Python: os.remove is not working

Why isn't os.remove(-string-) working for me? I have the code written as follows:

try:
os.remove(a)
    output = current_time() + "\trmv successful"
    message = message + '\n' + output
    message = "".join(message)
    return message

except OSError:
    try:
        os.removedirs(a)
        output = current_time() + "\trmv successful"
        message = message + '\n' + output
        message = "".join(message)
        return message

    except OSError:
        output = current_time() + "\trmv failed: [?]"
        message = message + '\n' + output
        message = "".join(message)
        return message

And it would return 21:32:53 rmv failed: [?] every time I perform the rmv command in the client. My Python version is 2.6.1 if that helps.

Upvotes: 10

Views: 26046

Answers (3)

Garry S
Garry S

Reputation: 89

try putting some delay time.sleep(0.2) after opening / removing files Or It seems a windows and/or antivirus issue

Josh Rosenberg on this error tracking on python development points out the same:

Short version: Indexing and anti-virus tools prevent deletion from occurring.

Longer version: DeleteFile (and all the stuff that ultimately devolves to DeleteFile) operate in a funny way on Windows. Internally, it opens a HANDLE to the file, marks it as pending deletion, and closes the HANDLE. If no one snuck in and grabbed another HANDLE to the file during that time, then the file is deleted when DeleteFile's hidden HANDLE is closed. Well designed anti-virus/indexing tools use oplocks ( http://blogs.msdn.com/b/oldnewthing/archive/2013/04/15/10410965.aspx ) so they can open a file, but seamlessly get out of the way if a normal process needs to take exclusive control of a file or delete it. Sadly "well-designed" is not a term usually associated with anti-virus tools, so errors like this are relatively commonplace.

Workarounds like using GetTempFileName() and MoveFile() to move the file out of the way will work, though I believe they introduce their own race conditions (the temp file itself is created but the HANDLE is closed immediately, which could mean a race to open the empty file by the bad anti-virus that would block MoveFile()).

Basically, if you're running on Windows, and you're using unfriendly anti-virus/indexing tools, there is no clean workaround that maintains the same behavior. You can't keep creating and deleting a file of the same name over and over without risking access denied errors.

That said, you could probably get the same results by opening and closing the file only once. Change from the original pseudocode:

Upvotes: 0

GreenGuerilla
GreenGuerilla

Reputation: 379

Why don't you try printing out the error?

try:
    os.remove(a)
    output = current_time() + "\trmv successful"
    message = message + '\n' + output
    message = "".join(message)
    return message

except OSError, e:
    print ("Failed to remove %s\nError is: %s" % (a,e))
    try:
        os.removedirs(a)
        output = current_time() + "\trmv successful"
        message = message + '\n' + output
        message = "".join(message)
        return message

    except OSError, e:
        print ("Failed twice to remove %s\nError is: %s" % (a,e))
        output = current_time() + "\trmv failed: [?]"
        message = message + '\n' + output
        message = "".join(message)
        return message

The error could be literally anything you see... A permissions issue for example?

Upvotes: 5

Jochen Ritzel
Jochen Ritzel

Reputation: 107676

Exceptions are there to be looked at! Check this:

try:
    os.remove(a)
except OSError as e: # name the Exception `e`
    print "Failed with:", e.strerror # look what it says
    print "Error code:", e.code 

Modify your code to display the error message and you'll know why it failed. The docs can help you.

Upvotes: 13

Related Questions