How do I get this piece to follow symlinks in python 2.6?
def load_recursive(self, path): for subdir, dirs, files in os.walk(path): for file in files: if file.endswith('.xml'): file_path = os.path.join(subdir, file) try: do_stuff(file_path) except: continue
os. walk() returns a list of three items. It contains the name of the root directory, a list of the names of the subdirectories, and a list of the filenames in the current directory.
The dirpath is a string for the path to the directory. The dirnames is a list of the names of the subdirectories in dirpath (excluding '. ' and '..'). The filenames is a list of the names of the non-directory files in dirpath.
walk() work in python ? OS. walk() generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
Description. Python method readlink() returns a string representing the path to which the symbolic link points. It may return an absolute or relative pathname.
Set followlinks
to True
. This is the fourth argument to the os.walk
method, reproduced below:
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
This option was added in Python 2.6.
EDIT 1
Be careful when using followlinks=True
. According to the documentation:
Note: Be aware that setting
followlinks
to True can lead to infinite recursion if a link points to a parent directory of itself. walk() does not keep track of the directories it visited already.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With