Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a symbolic link in python?

Tags:

python

symlink

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

like image 367
akshayc11 Avatar asked Jul 28 '12 11:07

akshayc11


People also ask

Does rm remove symlinks?

The rm command is the dedicated tool for deleting files and directories from the system. Because the symlink itself is a file, we can use the rm command to remove it. The following rm command will remove the symlink. To remove multiple symlinks, use rm as you would to remove multiple files.

How do I remove a path in Python?

remove() method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.

How do I get rid of Mklink?

To delete a symbolic link, treat it like any other directory or file. If you created a symbolic link using the command shown above, move to the root directory since it is "\Docs" and use the rmdir command. If you created a symbolic link (<SYMLINK>) of a file, to delete a symbolic link use the del command.


1 Answers

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

like image 145
samfrances Avatar answered Sep 18 '22 09:09

samfrances