Extract specified field value from filename using positional indexing
This task is nearly equivalent to another recent question.  My advice is that string analysis is usually best done using regular expressions.

import os
import re

def get_field_str(file_path, field):
    """
    Get the string value of field stored in file_path

    &gt;&gt;&gt; get_field_str('/path/to/file/Z998-5.asdf', 'session')
    '5'
    """
    pattern = re.compile('(?P&lt;place&gt;.)(?P&lt;year&gt;.{3})-(?P&lt;session&gt;.)\.')
    return pattern.search(os.path.basename(file_path)).group(field)


The regex can validate the filename as it works.  Feel free to adjust it to match your expectations.