os.walk()是否缺少指向目录的符号链接? [英] Is os.walk() missing symlinks to directories?

查看:137
本文介绍了os.walk()是否缺少指向目录的符号链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些文件,一些目录,一些文件符号链接和一些目录符号链接的目录.

I have a directory containing some files, some directories, some symlinks to files and some symlinks to directories.

当我在目录中使用followlinks = false进行os.walk()时,我获得了文件名和指向文件名列表中目录以及目录名列表中目录的符号链接.但是到目录的符号链接不会显示在任何地方.这是Python中的错误还是功能,还是我做错了什么?

When I do os.walk() with followlinks=false in the directory, I get the files and symlinks to files in the filenames list and the directories in the dirnames list. But the symlinks to directories does not show up anywhere. Is this a bug or a feature in Python, or am I doing something wrong?

我希望指向目录的符号链接显示在文件名列表中,因为它们不是目录而是符号链接,而其他符号链接(指向其他文件)则显示在文件名列表中.

I expect the symlinks to directories to show up in the filenames list, because they are not directories but symlinks, and the other symlinks (to other files) show up in the filenames list.

示例: 目录foo包含以下内容:

Example: The directory foo contains the following:

-rw-rw-r-- 4 rikno staff 136 Jan 14 11:10 firefox
lrwxr-xr-x 1 rikno staff   5 Jan 23 13:29 latex -> tetex
lrwxr-xr-x 2 rikno staff  68 Jan 14 11:10 mozilla -> firefox
drwxrwxr-x 3 rikno staff 102 Jan 23 13:29 tetex

我希望在第一次迭代中返回os.walk('foo')

I expect os.walk('foo') in the first iteration to return

('foo', ['tetex'], ['firefox', 'latex', 'mozilla'])

或至少

('foo', ['latex', 'tetex'], ['firefox', 'mozilla'])

但我得到的只是

('foo', ['tetex'], ['firefox', 'mozilla'])

而且我从没有得到关于符号链接乳胶(指向目录tex)的任何信息

and I never get anything about the symlink latex (which points to the directory tetex)

好,结果

('foo', ['latex', 'tetex'], ['firefox', 'mozilla'])

因此,指向目录的符号链接显示在目录名列表中.

So the symlink to the directory shows up in the dirnames list.

我首先希望到目录的符号链接位于文件名列表中,而从未在目录名列表中查找,并且在尝试代码和文件系统以查找链接在哪里或链接丢失"的原因时,我意外地混淆了我的结果.

I first expected the symlink to the directory to be in the filenames list and never looked in the dirnames list, and when experimenting with the code and the file system to find where the link was or why the link was "missing" I accidently mixed up my results.

很抱歉询问.

推荐答案

在我的计算机上运行的os.walk()确实显示了所有符号链接:

Running on my machine, os.walk() does show all the sym links:

>>> os.walk("foo").next()
('foo', ['tetex', 'latex'], ['mozilla', 'firefox'])
>>> os.walk("foo", followlinks=False).next()
('foo', ['tetex', 'latex'], ['mozilla', 'firefox'])

我在这里看到的唯一'问题'是符号链接出现在目录列表中而不是文件列表中.

The only 'problem' I see here is that the sym link appears in the list of directories and not in the list of files.

在大多数使用情况下,这是预期的行为,因为人们希望能够将文件列表中的所有条目视为文件,而不必检查它是否是符号链接.

In most use cases, this would be the expected behaviour since one would expect to be able to treat all entries in the file-list as files and not have to check if it is a sym-link or not.

此线程来自python-dev 简要讨论了这个问题.

This thread from python-dev briefly discusses the issue.

"......将符号链接指向目录 进入文件列表而不是子目录列表 更好(它将问题转移到不同的用例,例如 那些实际上想要读取文件内容的文件."

"... putting the symlinks-to-directories into the files list instead of the subdirectory list isn't really any better (it just moves the problem to different use cases, such as those that actually want to read the file contents)."

以及链接的问题页面:

"例如,计算一个目录下所有文件的行数 目录中,代码可能如下所示:

"For example to count the number of lines of all the files under a directory, a code could go like this:

for root, dirs, files in os.walk(top):
    for file in files:
        f = open(file)
        for n, l in enumerate(f, 1):
            pass
        print(file, n)

如果突然在文件中出现了指向目录的符号链接,这将 休息.因此,我不认为值得更改此设置.一个符号链接 目录离文件比离目录更近,它确实 取决于用例."

If, suddently, a symlink to a directory appeared in files, this will break. So I'm not convinced it's worth changing this. A symlink to a directory is not much closer to a file than to a directory, it really depends on the use case."

这篇关于os.walk()是否缺少指向目录的符号链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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