如何删除符号链接与源目录 [英] How to delete the symlink along with the source directory

查看:107
本文介绍了如何删除符号链接与源目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to delete the symlink along with the source directory.

例如 -

ls -lrt
testsymlink -> /user/temp/testdir

我想删除 testsymlink / user / temp / testdir 。考虑到我只知道符号链接名称。

I would like to remove both testsymlink and /user/temp/testdir. Consider that I know the only the symlink name.

任何使用python的工具都会做得很好。

Any utility with python will do great.

推荐答案

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

You can use the result of os.path.realpath to detect and delete the symlink target. Example:

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)

这篇关于如何删除符号链接与源目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆