-1

I'm attempting to find all files that contain certain variations of a codec in their name, but when I try to execute the following code, I get nothing but '* Exited normally *'

  • x264, X264, h264, H264
  • as above, but with a period between the X/H and 264
#!/usr/bin/python3

import os
import re

rootdir = "/mnt/local/int002/Downloads/Complete"
regex = re.compile('x264')

for root, dirs, files in os.walk(rootdir):
  for file in files:
    if regex.match(file):
       print(file)
aSystemOverload
  • 2,994
  • 18
  • 49
  • 73
  • 1
    Try `re.seach`? – rma Feb 17 '20 at 21:26
  • 3
    Not every string problem is a regex problem. What's wrong with [`"x264" in filename`](https://docs.python.org/3/reference/expressions.html#in)? – ChrisGPT was on strike Feb 17 '20 at 21:26
  • Because I need it more complex than that, I'm just starting with the basics. – aSystemOverload Feb 17 '20 at 21:27
  • 3
    `re.match` only matches at the beginning of the string - https://docs.python.org/3/library/re.html#re.match – wwii Feb 17 '20 at 21:28
  • 2
    Does this answer your question? [Python regular expression re.match, why this code does not work?](https://stackoverflow.com/questions/14933771/python-regular-expression-re-match-why-this-code-does-not-work) ..Or.. [What is the difference between re.search and re.match?](https://stackoverflow.com/questions/180986/what-is-the-difference-between-re-search-and-re-match) – wwii Feb 17 '20 at 21:29
  • I was so focused on the regex itself, I didn't even look at whether .match was correct or not. thank you – aSystemOverload Feb 17 '20 at 21:40

1 Answers1

0

If you really need to use a regex, try re.search. re.match only matches the beginning of the string.

#!/usr/bin/python3

import os
import re

rootdir = "/mnt/local/int002/Downloads/Complete"
regex = re.compile('x264')

for root, dirs, files in os.walk(rootdir):
  for file in files:
    if regex.search(file):
       print(file)
rma
  • 1,853
  • 1
  • 22
  • 42