1

I made a script with python that creates placeholders in all empty directories so that they wont get deleted.

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/'

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

John Bielowski
  • 199
  • 2
  • 11

1 Answers1

2

Use os.walk instead of glob, as in:

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. )

Roy2012
  • 11,755
  • 2
  • 22
  • 35