[英]python how to skip symlinks when checking folders
I made a script with python that creates placeholders in all empty directories so that they wont get deleted.我用 python 制作了一个脚本,它在所有空目录中创建占位符,这样它们就不会被删除。
The problem with that is: it detects symlinks which I do not want to happen.问题在于:它检测到我不想发生的符号链接。
and I am getting error like: PermissionError: [Errno 13] Permission denied: 'system/d/'
我收到如下错误:
PermissionError: [Errno 13] Permission denied: 'system/d/'
code:代码:
#!/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
How do i ignore symlinks我如何忽略符号链接
Use os.walk
instead of glob, as in:使用
os.walk
代替 glob,如下所示:
dirs.append(list(os.walk(path, followlinks=False)))
( Please note that according to the documentation , the default for followLinks
is False - so you can omit this argument. ) (请注意,根据文档,
followLinks
的默认值为 False - 因此您可以省略此参数。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.