Questions tagged [fnmatch]

`fnmatch` is the name for both a C-language standard library function for performing shell-style wildcard pattern-matching against strings, and one of many wrapper implementations exposing the mechanism of C-language `fnmatch` to a higher-level language – i.e. Python, Ruby, `awk`, &c.

fnmatch is both:

  1. A -language standard library function, fnmatch(…), and
  2. Any one of a number of many wrapper implementations exposing the mechanism of -language fnmatch to a higher-level language

One of the most noteworthy users of fnmatch(…) is – specifically, patterns defined in files are applied using fnmatch(…) – q.v. the [documentation]4 and the [source code]5 itself.

Questions about pattern-matching can often interchangeably refer to fnmatch patterns as well.

The full signature of the fnmatch(…) C function is:

int fnmatch(char const* pattern,
            char const* string,
                    int flags);

For details of its use, see the man page for fnmatch(3).

60 questions
1004
votes
28 answers

How to use to find files recursively?

I would like to list all files recursively in a directory. I currently have a directory structure like this: src/main.c src/dir/file1.c src/another-dir/file2.c src/another-dir/nested/files/file3.c I've tried to do the following: from glob import…
Ben Gartner
  • 14,413
  • 10
  • 36
  • 34
360
votes
30 answers

How to count the number of files in a directory using Python

How do I count only the files in a directory? This counts the directory itself as a file: len(glob.glob('*'))
prosseek
  • 182,215
  • 215
  • 566
  • 871
161
votes
13 answers

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

I want to open a series of subfolders in a folder and find some text files and print some lines of the text files. I am using this: configfiles = glob.glob('C:/Users/sam/Desktop/file1/*.txt') But this cannot access the subfolders as well. Does…
UserYmY
  • 8,034
  • 17
  • 57
  • 71
17
votes
2 answers

Python, how to implement something like .gitignore behavior

I need to list all files in the current directory (.) (including all sub directories), and exclude some files as how .gitignore works (http://git-scm.com/docs/gitignore) With fnmatch (https://docs.python.org/2/library/fnmatch.html) I will be able to…
fj123x
  • 6,904
  • 12
  • 46
  • 58
11
votes
2 answers

.gitignore style fnmatch()

What would be the simplest way to have .gitignore style fnmatch() with Python. Looks like that stdlib does not provide a match() function which would match a path spec against an UNIX style path regex. fnmatch() matches only pure filenames, no…
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
9
votes
4 answers

fnmatch and recursive path match with `**`

Is there any built-in or straightforward way to match paths recursively with double asterisk, e.g. like zsh does? For example, with path = 'foo/bar/ham/spam/eggs.py' I can use fnmatch to test it with fnmatch(path, 'foo/bar/ham/*/*.py' Although, I…
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
7
votes
4 answers

Github Branch protection rule, pattern for set branch names

i'm trying to set rules for set branches on my repo, but having issues with the pattern to apply only to specific branches. ie rule to apply only to brances master,develop,release Issue: the pattern isn't picking up the wrong branches I tried…
ASH
  • 980
  • 1
  • 9
  • 22
7
votes
5 answers

pathinfo vs fnmatch

There was a small debate regarding the speed of fnmatch over pathinfo here : how to check if file is php? I wasn't totally convinced so decided to benchmark the two functions. Using dynamic and static paths showed that pathinfo was faster. Is my…
zaf
  • 22,776
  • 12
  • 65
  • 95
5
votes
3 answers

Negating a fnmatch pattern

Consider this pattern: *.py. It matches all the paths ending with the py extension. Now, is it possible to find a pattern which matches everything else? I thought this would do it: *[!.][!p][!y], but apparently fnmatch.fnmatch always returns False…
rubik
  • 8,814
  • 9
  • 58
  • 88
4
votes
1 answer

What is the difference between a single /* and double /** trailing asterisk in a .gitignore file?

Consider the following two patterns in a .gitignore file foo/* foo/** The pattern format specification states: An asterisk * matches anything except a slash. [...] A trailing /** matches everything inside. For example, abc/** matches all files…
blu3r4y
  • 144
  • 1
  • 7
3
votes
2 answers

Get also elements that don't match fnmatch

I'm using a recursive glob to find and copy files from a drive to another def recursive_glob(treeroot, pattern): results = [] for base, dirs, files in os.walk(treeroot): goodfiles = fnmatch.filter(files, pattern) …
LarsVegas
  • 6,522
  • 10
  • 43
  • 67
2
votes
2 answers

fnmatch not finding "&" character

I'm relatively new to Python, so sorry if I butcher any terms or make dumb questions. My goal here is to rename any filename in a specific directory containing the string "&" to an "x". So far I've tried making python look for these files and print…
2
votes
0 answers

Python: how to handle int'l chararacters in filenames for glob and fnmatch?

I want to loop over all files in a directory. I tried glob: from glob import glob for filename in glob('*'): print filename and I tried fnmatch: import fnmatch import os for file in os.listdir('.'): if fnmatch.fnmatch(file, '*'): …
jamacoe
  • 519
  • 4
  • 16
2
votes
2 answers

Speed up file matching based on names of files

so I have 2 directories with 2 different file types (eg .csv, .png) but with the same basename (eg 1001_12_15.csv, 1001_12_15.png). I have many thousands of files in each directory. What I want to do is to get the full paths of files, after having…
Andreas Alamanos
  • 348
  • 3
  • 11
2
votes
3 answers

Exclude files with fnmatch

I am creating a loop in Python that runs through all files in specific directory using fnmatch.filter(). Currently I am going through all .csv files in specific folder as following: for file_source in fnmatch.filter(os.listdir(source_dir),…
Bostjan
  • 1,455
  • 3
  • 14
  • 22
1
2 3 4