I have written code that works for my cause and displays the results in the terminal:
print 'The following results are found:'
# some code iterations here
...
...
print 'User - {0}, Title - {1}'.format(...)
Currently, I am trying to implement a new optional argument such that it allows me to choose whether I would like the above results to be written to a text file.
While I can get it to work, it is not in the most elegant method:
# output_to_path is a boolean argument here.
if output_to_file:
# file_path, I use `open(file_dir, "w")`
print >> file_path, 'The following results are found:'
print 'The following results are found:'
# some code iterations here
...
...
if output_to_file:
print 'User - {0}, Title - {1}'.format(...)
print 'User - {0}, Title - {1}'.format(...)
Is it possible to only write the above print statements once, whether output_to_file
is true or false? I ask as I do have a ton of print statements to begin with.