matnum.py 1.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import os


def find_file(pattern, path):
    """Finds file given pattern

    Args:
        pattern (str): pattern
        path (str): path to folder

    Returns:
        list: list of filenames in folder matching pattern
    """

15
16
17
18
19
20
21
    import fnmatch

    result = []
    for root, _, files in os.walk(path):
        for name in files:
            if fnmatch.fnmatch(name, pattern):
                result.append(os.path.join(root, name))
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

    return result


def check_matnum(matnum):
    """Checks for valid matriculation number

    Args:
        matnum (str): matriculation number

    Returns:
        bool: valid
    """
    return len(matnum) == 6 and matnum.isdigit()


def get_matnum(s):
    """Extracts matriculation number from string

    Args:
    s (str): file name with first 6 characters matriculation number

    Returns:
    str: 6-digit matriculation number
    """

    # Get matriculation number
    # Has to be separated by "_" from the rest of the file name
    matnum = s.split('_', 1)[0]

    # Sanity check
    if not check_matnum(matnum):
        raise ValueError("{} not a valid matriculation number".format(matnum))

    return matnum