Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Print to Text file in Python 3
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!
You can do
from pathlib import Path
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)    
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Print/write to file function tpolim008 4 224 Apr-01-2020, 07:59 PM
Last Post: tpolim008
  Print date, Time and output to file tpolim008 3 192 Mar-26-2020, 06:49 PM
Last Post: ndc85430
  How to extract specific rows and columns from a text file with Python Farhan 0 237 Mar-25-2020, 09:18 PM
Last Post: Farhan
  Look for match in two files and print out in the first file Batistuta 0 175 Mar-03-2020, 02:27 PM
Last Post: Batistuta
  Rename file from value in text file Nuge93 1 261 Jan-20-2020, 03:50 PM
Last Post: gruntfutuk
  auto supply values to a python script(function) from text file metro17 4 396 Oct-26-2019, 01:25 AM
Last Post: ichabod801
  Trying to make column based file from text file scor1pion 7 623 Jul-16-2019, 02:43 PM
Last Post: scor1pion
  How best to format text in a simple text file? Pedroski55 2 898 Jun-14-2019, 06:07 AM
Last Post: noisefloor
  Print a JSON file mrobie 5 789 May-31-2019, 09:38 PM
Last Post: Yoriz
  Print random musik to wav file Pereskia 2 583 Mar-27-2019, 11:53 AM
Last Post: Pereskia

Forum Jump:

Users browsing this thread: 1 Guest(s)