<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def filenames(directory, tag='', sorted=False, recursive=False):
 """ Reads in all filenames from a directory that contain a specified substring. Parameters directory : :obj:`str` the directory to read from tag : :obj:`str` optional tag to match in the filenames sorted : bool whether or not to sort the filenames recursive : bool whether or not to search for the files recursively Returns ------- :obj:`list` of :obj:`str` filenames to read from """

    if recursive:
        f = [os.path.join(directory, f) for directory, _, filename in os.walk(directory) for f in filename if f.find(tag) > -1] 
    else:
        f = [os.path.join(directory, f) for f in os.listdir(directory) if f.find(tag) > -1]
    if sorted:
        f.sort()
    return f