Why is cmake file GLOB evil?

The problem is when you’re not alone working on a project. Let’s say project has developer A and B. A adds a new source file x.c. He doesn’t changes CMakeLists.txt and commits after he’s finished implementing x.c. Now B does a git pull, and since there have been no modifications to the CMakeLists.txt, CMake isn’t … Read more

How can I search sub-folders using glob.glob module? [duplicate]

In Python 3.5 and newer use the new recursive **/ functionality: configfiles = glob.glob(‘C:/Users/sam/Desktop/file1/**/*.txt’, recursive=True) When recursive is set, ** followed by a path separator matches 0 or more subdirectories. In earlier Python versions, glob.glob() cannot list files in subdirectories recursively. In that case I’d use os.walk() combined with fnmatch.filter() instead: import os import fnmatch … Read more

How are glob.glob()’s return values ordered?

Order is arbitrary, but you can sort them yourself If you want sorted by name: sorted(glob.glob(‘*.png’)) sorted by modification time: import os sorted(glob.glob(‘*.png’), key=os.path.getmtime) sorted by size: import os sorted(glob.glob(‘*.png’), key=os.path.getsize) etc.

Python glob multiple filetypes

Maybe there is a better way, but how about: import glob types = (‘*.pdf’, ‘*.cpp’) # the tuple of file types files_grabbed = [] for files in types: files_grabbed.extend(glob.glob(files)) # files_grabbed is the list of pdf and cpp files Perhaps there is another way, so wait in case someone else comes up with a better … Read more

How to loop over files in directory and change path and add suffix to filename

A couple of notes first: when you use Data/data1.txt as an argument, should it really be /Data/data1.txt (with a leading slash)? Also, should the outer loop scan only for .txt files, or all files in /Data? Here’s an answer, assuming /Data/data1.txt and .txt files only: #!/bin/bash for filename in /Data/*.txt; do for ((i=0; i<=3; i++)); … Read more

Test whether a glob has any matches in Bash

Bash-specific solution: compgen -G “<glob-pattern>” Escape the pattern or it’ll get pre-expanded into matches. Exit status is: 1 for no-match, 0 for ‘one or more matches’ stdout is a list of files matching the glob. I think this is the best option in terms of conciseness and minimizing potential side effects. Example: if compgen -G … Read more

How to use glob() to find files recursively?

pathlib.Path.rglob Use pathlib.Path.rglob from the the pathlib module, which was introduced in Python 3.5. from pathlib import Path for path in Path(‘src’).rglob(‘*.c’): print(path.name) If you don’t want to use pathlib, use can use glob.glob(‘**/*.c’), but don’t forget to pass in the recursive keyword parameter and it will use inordinate amount of time on large directories. … Read more