Checking the health of os.remove

To delete a file in Python , I use os.remove .

Documents (related) do not indicate any exceptions except OSError :

If the path is a directory, an OSError is raised

How to check for exceptions like FileNotFound , PermissionToDeleteDenied , etc.? Or is such an error check not performed by the os.remove function (the documents for os.remove and os.unlink do not seem to say)?

+4
source share
2 answers

OSError exceptions have an errno attribute, which you can use with the errno module to get more information about what type of OS error has occurred. See the documentation for OSError .

+9
source

Use this code:

 import os if(os.path.exists("c:/randomDirectory/random.txt"): # some random code 

it runs random code if random.txt exists.

-5
source

All Articles