0

I need to update hundreds of symlinks in a nested directory. I tried using os.walk with followlinks=True but it was giving me subdirectories and stuff.

Let's say I have the following directory structure:

output/
output/subdirectory_1/file_1.txt
output/subdirectory_2/file_2.txt
output/subdirectory_2/symlink_1.txt
output/subdirectory_2/subdirectory_2a/symlink_2.txt
output/symlink_3.txt
output/file_3.txt

How can I use os.walk (or something else in the standard library) that can give me something similar to glob? That is, like the following (order doesn't matter):

for fp in [INSERT FUNCTION]:
    print(fp)


output/subdirectory_2/symlink_1.txt
output/subdirectory_2/subdirectory_2a/symlink_2.txt
output/symlink_3.txt

EDIT: This is my current code, is this the preferred method?

In [8]: symlinks = list()
   ...: for i, path in pv(enumerate(os.walk(".", followlinks=True))):
   ...:     parent_directory, subdirectories, files = path
   ...:     for name in subdirectories + files:
   ...:         path_child = os.path.join(parent_directory, name)
   ...:         if os.path.islink(path_child):
   ...:             print(path_child)
O.rka
  • 29,847
  • 68
  • 194
  • 309

0 Answers0