Even if it's strange even other filesystems objects can have " extensions", folders too.The globbing method is empirically proven to be slower than most other methods.The former point results in obtaining incorrect results depending on file extensions.The globbing " language" does not allows perfect specification of multiple extension.In practice, you just need to remember that as long as you surround ** with forward slashes (path separators), it matches zero or more directories.īy the results I've obtained from empirical tests, it turned out that glob.glob isn't the better way to filter out files by their extensions. This will invoke glob.glob('/**/*.txt', recursive=True) and so on for each extension.ġ Technically, the ** glob symbol simply matches one or more characters including forward-slash / (unlike the singular * glob symbol). ![]() ![]() If you need to search not just a single directory, but also all sub-directories, you can pass recursive=True and use the multi-directory glob symbol ** 1: files = But I would argue that it is usually unnecessary to factor out a single line to a separate function, which is why I'm providing this as an alternative solution. The list-comprehension is the same used in Laurent's solution (which I've voted for). This solution allows you to have a custom list of exts that can be changed without having to update your code. This solution is short, and without any unnecessary for-loops, nested list-comprehensions, or functions to clutter the code. You loop over the extensions ( for ext in exts), and then for each extension you take each file matching the glob pattern ( for f in glob.glob(os.path.join(project_dir, ext)). Here is one-line list-comprehension variant of Pat's answer (which also includes that you wanted to glob in a specific project directory): import os, globĮxts = įiles =
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |