Python Glob.glob: a wildcard for the number of directories between the root and the destination

2024/10/10 10:23:26

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!

Answer

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)
https://en.xdnf.cn/q/69907.html

Related Q&A

Get datetime format from string python

In Python there are multiple DateTime parsers which can parse a date string automatically without providing the datetime format. My problem is that I dont need to cast the datetime, I only need the dat…

Generating an optimal binary search tree (Cormen)

Im reading Cormen et al., Introduction to Algorithms (3rd ed.) (PDF), section 15.4 on optimal binary search trees, but am having some trouble implementing the pseudocode for the optimal_bst function in…

Pydub from_mp3 gives [Errno 2] No such file or directory

I find myself in front of a wall here, simply trying to load an audio file into pydub for converting it keeps on throwing a "[Errno 2] No such file or directory" error.Naturally I have spent …

Compile Python 3.6.2 on Debian Jessie segfaults on sharedmods

Im trying to compile Python 3.6.2 on a Debian Jessie box with the options./configure --prefix="/opt/python3" \ --enable-optimizations \--with-lto \ --enable-profiling \ --enable-unicode=ucs4 …

Escape space in filepath

Im trying to write a python tool that will read a logfile and process itOne thing it should do is use the paths listed in the logfile (its a logfile for a backup tool)/Volumes/Live_Jobs/Live_Jobs/*SCAN…

How to save web page as text file?

I would like to save a web page (all content) as a text file. (As if you did right click on webpage -> "Save Page As" -> "Save as text file" and not as html file)I have tried …

PIL Image.size returns the opposite width/height

Using PIL to determine width and height of imagesOn a specific image (luckily only this one - but it is troubling) the width/height returning from image.size is the opposite. The image: http://storage.…

Django Rest Framework: Register multiple serializers in ViewSet

Im trying to create a custom API (not using models), but its not showing the request definition in the schema (in consequence, not showing it in swagger). My current code is:views.pyclass InfoViewSet(v…

Which is most accurate way to distinguish one of 8 colors?

Imagine we how some basic colors:RED = Color ((196, 2, 51), "RED") ORANGE = Color ((255, 165, 0), "ORANGE") YELLOW = Color ((255, 205, 0), "YELLOW") GREEN = Color ((0, 128…

Lambda function behavior with and without keyword arguments

I am using lambda functions for GUI programming with tkinter. Recently I got stuck when implementing buttons that open files: self.file="" button = Button(conf_f, text="Tools opt.",…