How to delete a symbolic link in python?
The accepted answer does not work on Windows with links created via mklink /D
. If that is your problem the answer has been posted in this question: Delete Symlink to directory on Windows
The following code should work on both systems:
if(os.path.isdir(targetLink)): os.rmdir(targetLink)else: os.unlink(targetLink)
Sorry,my Bad, I had made a stupid programming mistake : I was stupidly deleting the source instead of the links.
The correct answer is by @samfrances.
os.unlink
does the trick.
In addition to this, here some other tips if you want to clear a directory using python:
Definitely not threadsafe, but you get the idea...
def rm(obj): if os.path.exists(obj): if os.path.isdir(obj): if os.path.islink(obj): os.unlink(obj) else: shutil.rmtree(obj) else: if os.path.islink(obj): os.unlink(obj) else: os.remove(obj)