Python: os.remove not working

Why doesn't it work for me os.remove(-string-)? I have 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 will return 21:32:53 rmv failed: [?]every time I execute a command rmvon the client. My version of Python is 2.6.1, if that helps.

+5
source share
3 answers

Exceptions must be considered! 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 

Change your code to display an error message and you will understand why this failed. Documents can help you.

+7
source

Why aren't you trying to print 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 can be literally any that you see ... For example, a problem with permissions?

+3
source

time.sleep(0.2) / , Windows /

python :

: .

: DeleteFile ( , DeleteFile) Windows. , . , , DeleteFile hidden HANDLE . / oplocks (http://blogs.msdn.com/b/oldnewthing/archive/2013/04/15/10410965.aspx), , , . , " " , , , , .

, GetTempFileName() MoveFile() , , , ( temp , HANDLE , , , MoveFile()).

, Windows / , , . , .

, , . :

0

All Articles