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.
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.
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.
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With