如何删除符号链接以及源目录

Bal*_*aji 3 python directory shell

我想删除符号链接以及源目录.

例如 -

ls -lrt
testsymlink -> /user/temp/testdir
Run Code Online (Sandbox Code Playgroud)

我想删除testsymlink/user/temp/testdir.考虑一下,我知道唯一的符号链接名称.

使用python的任何实用程序都会很棒.

Tim*_*ais 5

您可以使用结果os.path.realpath来检测和删除符号链接目标.例:

import os

# ./foo -> ./bar
filepath = "./foo"

if (os.path.realpath(filepath) != filepath):
    targetpath = os.path.realpath(filepath)

os.remove(filepath)
if (targetpath):
     os.remove(targetpath)
Run Code Online (Sandbox Code Playgroud)