How to delete a symbolic link in python?
Asked Answered
W

5

43

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

Who answered 28/7, 2012 at 11:18 Comment(6)
Strange, os.remove() does not remove the original file for me, only the symlink (tried with Python 2.7.2 and 3.1.4).Concrescence
It does not remove the original file neither at my system (Python 2.7.1)Aurita
Smth worth-looking on symlinks: https://mcmap.net/q/390660/-how-to-delete-the-symlink-along-with-the-source-directory (note: shutil.rmtree would fail to remove a link, despite os.path.osdir() saying the path pertains to a dir)Cleek
The question is about links to directories, not links to files. Removing links to directories does not work for me either. Python 2.6Irvine
What is os.shutil?Ferdinand
@Irvine Removing links to directories works for me. Python 2.7.5, CentOS 7Hal
A
59

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

Allhallows answered 28/7, 2012 at 21:56 Comment(3)
Python 2.7 documentation say about unlink: This is the same function as remove();, how did it solved the problem?Someone
This solution does not work on Windows for directory links created via mklink /D. See my answer below if that's your problem.Irvine
How does it work recursively to all symbolic links in a directory?Foretopsail
I
10

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)
Irvine answered 5/1, 2016 at 7:15 Comment(0)
W
7

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)
Who answered 29/7, 2012 at 10:0 Comment(1)
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.Permission
N
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()
Np answered 22/2, 2019 at 15:48 Comment(0)
L
1

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.

Lafreniere answered 28/7, 2012 at 11:30 Comment(4)
It says OSError: [Errno 1] Operation not permitted: 'test/' for me. Probably it's implementation dependent.Aurita
I don't have python handy. I should have said that this was a guess. I've edited the answer.Lafreniere
@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 issueWho
@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...Who

© 2022 - 2024 — McMap. All rights reserved.