Matching and parsing file names like IMG_####.JPG or IMG_%04d.JPG
Project description
framefile
Python library for parsing and matching file name patterns like IMG_####.JPG
or
IMG_%04d.JPG
.
Such patterns are used, for example, for the file names of individual video frames in ffmpeg and Blender.
Install
pip3 install framefile
other options
Install pre-release from GitHub:
pip3 install git+https://github.com/rtmigo/framefile_py@staging#egg=framefile
Use
Guess pattern from file name
import framefile
print(framefile.filename_to_hash_pattern("IMG_4567.JPG")) # IMG_####.JPG
print(framefile.filename_to_pct_pattern("IMG_4567.JPG")) # IMG_%04d.JPG
Find files by pattern
import glob
import framefile
file_mask = framefile.hash_pattern_to_glob('/path/to/img####.jpg')
print(glob.glob(file_mask))
# prints all files matching /path/to/img####.jpg
For percent patterns pct_pattern_to_glob
can be used instead of hash_pattern_to_glob
.
Match file names as strings
import re
import framefile
regex = framefile.hash_pattern_to_regex('img####.jpg')
a = re.match(regex, 'img0023.jpg')
print(a.group(0)) # img0023.jpg
print(a.group(1)) # 0023
b = re.match(regex, 'anything.txt')
print(b) # None
For percent patterns pct_pattern_to_regex
can be used instead of hash_pattern_to_regex
.
Extract number from file name
import framefile
x: int = framefile.hash_extract_number("img####.jpg", "img0023.jpg")
print(x) # 23
try:
y = framefile.hash_extract_number("img####.jpg", "thumbs.db")
except framefile.PatternMismatchError:
print("Oops!")
For percent patterns pct_extract_number
can be used instead of hash_extract_number
.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
framefile-0.0.5.tar.gz
(4.3 kB
view hashes)
Built Distribution
Close
Hashes for framefile-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19b61f0be032099f52575b15f1f1240c03a44f2ab7b668fffbe97a304d84a95c |
|
MD5 | 3c5f37b79b19d2ea4dacdf8a46fdbe79 |
|
BLAKE2b-256 | 64cd67832d752b1e7832c758e296925a98ef5fc8cdc3b1a79a14d00cddece2a0 |