import io import time from line_profiler import LineProfiler def test1(args): print(args) time.sleep(2) return 1 if __name__ == "__main__": lp = LineProfiler() lp_wrap = lp(test1) result = lp_wrap("123123") print(result) output = io.StringIO() lp.print_stats(stream=output) with open(f"test.txt", 'w') as f: f.write(output.getvalue())