无法删除/取消链接到 python & 中的目录的符号链接视窗 [英] Can't delete / unlink a symlink to directory in python & windows

查看:39
本文介绍了无法删除/取消链接到 python & 中的目录的符号链接视窗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已编辑

我在 Widnows7 上使用 mklink 命令行创建了指向目录的符号链接:

mklink/d 书籍配置

我正在尝试使用 python 2.7(仍在 Windows 上)删除它.

<预><代码>>>>os.remove('书籍')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中sym = symlink_to_diros.unlink(sym) #WindowsError:[错误 5] 访问被拒绝:书籍"

那台机器没有限制,我是管理员,而且我从 Windows 中删除它没有问题 (del books)

删除指向文件的链接(而不是目录)没有问题.

这是为什么?

edit "del" 不起作用,只是没有返回错误.

解决方案

糟糕,我忽略了:

由于它是指向目录的链接,因此与 linux 不同,windows 将符号链接视为目录,因此:

来自 DOS:

c:\>rmdir 符号链接

来自蟒蛇:

<预><代码>>>>os.rmdir('符号链接')

NOTdel symlink",os.unlink()",os.remove()".

这是在 Linux 中的样子:

$ mkdir a$ln -s a b$ rm b #ok,因为符号链接被视为文件$ln -s a b$ rmdir b # 错误,不是文件rmdir: 无法删除 `b': 不是目录

edited

i created symlinks to a directory, on Widnows7, using mklink command line:

mklink /d books config

i'm trying to delete it with python 2.7 (still on windows).

>>> os.remove('books')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    sym = symlink_to_dir
    os.unlink(sym) # 
WindowsError: [Error 5] Access is denied: 'books'

there are no restrictions on that machine, i'm admin, and i didn't have problems to delete it from Windows (del books)

There's no problem deleting a link to a file (as opposed to a dir).

why is that?

edit "del" didn't work, it just didn't return an error.

解决方案

oops, i overlooked it:

since it's a link to a directory, windows, unlike linux, consider the symlink as a directory, therefore:

from DOS:

c:\> rmdir symlink

from python:

>>> os.rmdir( 'symlink' )

and NOT "del symlink", nor "os.unlink()", nor "os.remove()".

This is how it looks like in Linux:

$ mkdir a
$ ln -s a b
$ rm b          #ok, since a symlink is treated as a file

$ ln -s a b
$ rmdir b       # error, not a file
rmdir: failed to remove `b': Not a directory

这篇关于无法删除/取消链接到 python &amp; 中的目录的符号链接视窗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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