Okay I'm having trouble not only with the problem itself but even with trying to explain my question. I have a directory tree consisting of about 7 iterations, so: rootdir/a/b/c/d/e/f/destinationdir
The thing is some may have 5 subdirectory levels and some may have as many as ten, such as:
rootdir/a/b/c/d/destinationdir
or:
rootdir/a/b/c/d/e/f/g/h/destinationdir
The only thing they have in common is that the destination directory is always named the same thing. The way I'm using the glob function is as follows:
for path in glob.glob('/rootdir/*/*/*/*/*/*/destinationdir'):
--- os.system('cd {0}; do whatever'.format(path))
However, this only works for the directories with that precise number of intermediate subdirectories. Is there any way for me not to have to specify that number of subdirectories(asterices)
; in other words having the function arrive at the destinationdir no matter what the number of intermediate subdirectories is, and allowing me to iterate through them. Thanks a lot!
Python glob. glob() method returns a list of files or folders that matches the path specified in the pathname argument. This function takes two arguments, namely pathname, and recursive flag. pathname : Absolute (with full path and the file name) or relative (with UNIX shell-style wildcards).
We can use the function glob. glob() or glob. iglob() directly from glob module to retrieve paths recursively from inside the directories/files and subdirectories/subfiles. Note: When recursive is set True “ ** ” followed by path separator ('./**/') will match any files or directories.
The glob. glob returns the list of files with their full path (unlike os. listdir()) and is more powerful than os. listdir that does not use wildcards.
Glob in Python. glob is a powerful tool in Python to help with file management and filtering. While os helps manage and create specific paths that are friendly to whatever machine they are used on, glob helps to filter through large datasets and pull out only files that are of interest.
I think this could be done more easily with os.walk
:
def find_files(root,filename):
for directory,subdirs,files in os.walk(root):
if filename in files:
yield os.join(root,directory,filename)
Of course, this doesn't allow you to have a glob expression in the filename portion, but you could check that stuff using regex or fnmatch.
EDIT
Or to find a directory:
def find_files(root,d):
for directory,subdirs,files in os.walk(root):
if d in subdirs:
yield os.join(root,directory,d)
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