Skip to content

Instantly share code, notes, and snippets.

@promto-c
Created February 29, 2024 13:21
Show Gist options
  • Select an option

  • Save promto-c/f59f9c8281fd0faa1cd84b420a16bfec to your computer and use it in GitHub Desktop.

Select an option

Save promto-c/f59f9c8281fd0faa1cd84b420a16bfec to your computer and use it in GitHub Desktop.
Python Script to Extract File Sequence Details
import re
from typing import Dict
def extract_file_details(input_string: str) -> Dict[str, str]:
"""Extracts directory name, file name, frame start, frame end, and extension from the input string.
Args:
input_string: A string in the format 'path/to/plates/plates_name.[1001-1017].ext'
Returns:
A dictionary with keys 'directory_name', 'file_name', 'frame_start', 'frame_end', 'extension'.
Returns an empty dictionary if the input string does not match the expected format.
>>> extract_file_details('path/to/plates/plates_name.[1001-1017].ext')
{'directory_name': 'path/to/plates', 'file_name': 'plates_name', 'frame_start': '1001', 'frame_end': '1017', 'extension': 'ext'}
"""
pattern = r'(?P<directory_name>.*)/(?P<file_name>.*)\.\[(?P<frame_start>\d+)-(?P<frame_end>\d+)\]\.(?P<extension>[a-zA-Z0-9]+)$'
match = re.match(pattern, input_string)
if match:
return match.groupdict()
else:
return {}
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment