1

I'm using Python 2.7

I have a scenario like this

 for (root,dirs,files) in os.walk(dir_tree, topdown=True, followlinks=True):
        for atom in files:
            # do something with files
        for dir in dirs:
            # do something with dirs

I am performing an os.walk on a directory tree. This tree has some links. some of these links point to other locations inside the directory tree. Some of the links do not. I want os.walk to only follow links that point to other location in the directory tree

eg:

A/
   temp/
       collateral_file_1
   B/
       link1    ->   Somewhere outside A
       link2    ->   A/temp/
   C/
       file_1

In the above case dir_tree would A/ . When performing an os.walk on A I would like to explore link2 as it is a link to another location within A. but not link1.

You might wonder why? The contents of link1 would be explore in a non symlink following walk anyway. But the reason for this I am checking some file patterns that are fed in via a config file for certain properties.

eg: All files in A/B/link1/ should have 700 permissions etc.

Is there a way I could do this?

I thought about os.listdir but it seems like there should be an easier way

Srini
  • 1,619
  • 1
  • 19
  • 34

0 Answers0