39

I have been trying to delete some symbolic links in my working directory, but I am facing some issues.

os.remove also removes the actual contents of the original folder of the link

os.shutil throws up an error in case of symbolic links.

Is there a way to remove a symbolic link using python commands without destroying the original content?

Thanks

akshayc11
  • 515
  • 1
  • 4
  • 9

5 Answers5

55

os.unlink() works for me. It removes the symlink without removing the directory that it links to.

knia
  • 463
  • 6
  • 16
samfrances
  • 3,405
  • 3
  • 25
  • 40
11

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)
Community
  • 1
  • 1
MOnsDaR
  • 8,401
  • 8
  • 49
  • 70
6

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)
akshayc11
  • 515
  • 1
  • 4
  • 9
  • do `if os.path.exists(obj)` then `if os.path.islink` then `if os.path.isdir`. that way you don't need to have os.unlink twice. – QxQ Apr 28 '13 at 04:32
3

in Python 3.4 and above, If link is a file, use unlink().

>>> from pathlib import Path
>>> p = Path('/some/file/')
>>> p.unlink()

If the path points to a directory, use Path.rmdir() instead.

>>> from pathlib import Path
>>> p = Path('/some/dir/')
>>> p.rmdir()
SuperNova
  • 25,512
  • 7
  • 93
  • 64
2

If the directory name contains a trailing slash, the linux rm command will follow the link and try to delete the directory. See Remove a symlink to a directory. The os.remove documentation says that it will give you an OSError if you try to remove a directory but maybe that doesn't always happen in the case of symlinks.

Community
  • 1
  • 1
John Watts
  • 8,717
  • 1
  • 31
  • 35
  • 1
    It says `OSError: [Errno 1] Operation not permitted: 'test/'` for me. Probably it's implementation dependent. – KARASZI István Jul 28 '12 at 11:32
  • 1
    I don't have python handy. I should have said that this was a guess. I've edited the answer. – John Watts Jul 28 '12 at 12:00
  • @JohnWatts Your answer is true, but this holds true only for symlinks created by the shell. symlinks created by `os.symlink` still give the same issue – akshayc11 Jul 29 '12 at 09:43
  • 1
    @all: Edit:: Sorry... My Bad... I had made a stupid programming mistake... Was stupidly deleting the source instead of the links...gahhh... wasted almost a day on this... – akshayc11 Jul 29 '12 at 09:50