'Python how to export a print view to txt

I have a print that works perfectly to me since I use tabulate:

print(tabulate(query, headers= 'keys', tablefmt = 'psql'))

And I would like to export this print to a txt file just the way it looks in terminal.

Thanks for your support!



Solution 1:[1]

Try in this way

print(tabulate(query, headers= 'keys', tablefmt = 'psql'), file=open("output.txt", "a"))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Evren