Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: os.remove is not working

Tags:

python

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.

like image 440
Eugene Avatar asked Feb 15 '13 13:02

Eugene


People also ask

How do I remove an operating system from Python?

In Python, you can use the os. remove() method to remove files, and the os. rmdir() method to delete an empty folder. If you want to delete a folder with all of its files, you can use the shutil.

How do I force delete a directory in Python?

rmdir() method. os. rmdir() method in Python is used to remove or delete an empty directory. OSError will be raised if the specified path is not an empty directory.

How do I permanently delete Python?

Navigate to Control Panel. Click “Uninstall a program”, and a list of all the currently installed programs will display. Select the Python version that you want to uninstall, then click the “Uninstall” button above the list – this has to be done for every Python version installed on the system.


2 Answers

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.

like image 183
Jochen Ritzel Avatar answered Oct 16 '22 16:10

Jochen Ritzel


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?

like image 26
GreenGuerilla Avatar answered Oct 16 '22 16:10

GreenGuerilla