Print to Text file in Python 3 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Print to Text file in Python 3 (/thread-17972.html) |
Print to Text file in Python 3 - iteachpc - May-01-2019 How do I save the values a variable to a text file in Python 3? i.e. my_varialbe = 'This is the text content" write my_variable to my_variable.txt Thank you! RE: Print to Text file in Python 3 - Gribouillis - May-01-2019 You can do from pathlib import Path Path('my_variable.txt').write_text(my_variable) RE: Print to Text file in Python 3 - perfringo - May-02-2019 Is file already existing and have content? If existing and with content - do you want to write it to new line or at the end of existing line? One way to write (append) to file: >>> my_variable = 'This is the text content.' >>> with open('my_variable.txt', 'a') as f: ... f.write(my_variable) |