【问题标题】:python how to skip symlinks when checking folderspython在检查文件夹时如何跳过符号链接
【发布时间】:2020-09-19 22:02:03
【问题描述】:

我用 python 编写了一个脚本,在所有空目录中创建占位符,这样它们就不会被删除。

问题在于:它会检测到我不想发生的符号链接。

我收到如下错误:PermissionError: [Errno 13] Permission denied: 'system/d/'

代码:

#!/usr/bin/env python3

import os, glob

dirs = []
paths = ["system", "vendor"]
for path in paths:
    dirs.append(glob.glob(path + '/**/', recursive=True))

for dir in dirs:
    for dir1 in dir:
        if len(os.listdir(dir1)) == 0:
            open(dir1, '.PLACEHOLDER').close()
        else: continue

如何忽略符号链接

【问题讨论】:

标签: python


【解决方案1】:

使用os.walk而不是glob,如:

dirs.append(list(os.walk(path, followlinks=False)))

(请注意,根据to the documentationfollowLinks 的默认值为 False - 因此您可以省略此参数。)

【讨论】:

    猜你喜欢
    • 2012-12-14
    • 2012-06-19
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    相关资源
    最近更新 更多