Our last function, luckily, is a whole lot simpler than the previous one. This function provides output of the signature and filename in one of the supported formats. In the past, we've written separate functions to handle separate formats, though in this case we've opted to place them all in the same function. This design decision is because we want to provide results in near-real time, especially if the user is processing a number of files. Since our logs are redirected to STDERR, we can use the print() function to provide results on STDOUT. This allows flexibility to our users, who can pipe the output into another program (such as grep) and perform additional processing on the results:
188 def output(sigval, filename, output_type='txt'):
189 """Write the output of the script in the specified format
190 :param sigval (str): Calculated hash
191 :param filename (str): name of the file processed
192 :param output_type (str): Formatter to use for output
193 """
194 if output_type == 'txt':
195 print("{} {}".format(sigval, filename))
196 elif output_type == 'json':
197 print(json.dumps({"sig": sigval, "file": filename}))
198 elif output_type == 'csv':
199 print("{},\"{}\"".format(sigval, filename))
200 else:
201 raise NotImplementedError(
202 "Unsupported output type: {}".format(output_type))