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

Life of Brian: Conjugate the verb, "to go" !

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Trying to make column based file from text file scor1pion 7 279 Jul-16-2019, 02:43 PM
Last Post: scor1pion
  How best to format text in a simple text file? Pedroski55 2 535 Jun-14-2019, 06:07 AM
Last Post: noisefloor
  Print a JSON file mrobie 5 412 May-31-2019, 09:38 PM
Last Post: Yoriz
  Print random musik to wav file Pereskia 2 305 Mar-27-2019, 11:53 AM
Last Post: Pereskia
  write each line of a text file into separate text files and save with different names Shameendra 3 373 Feb-20-2019, 09:07 AM
Last Post: buran
  Saving python cmd output into a text file with colours kapilan15 2 424 Jan-25-2019, 06:25 PM
Last Post: metulburr
  Help with try and open 6.txt file and print as perfect or not Pleiades 13 779 Jan-03-2019, 10:14 PM
Last Post: Pleiades
  How do i read particular text from text file and update those values in MS SQL table ganeshsai2912 33 2,903 Nov-20-2018, 12:55 AM
Last Post: ganeshsai2912
  How to print a File Path ? Astrikor 3 761 Sep-24-2018, 10:18 AM
Last Post: Astrikor
  Parsing Text file having repeated value key pair using python manussnair 3 589 Aug-04-2018, 11:48 PM
Last Post: micseydel

Forum Jump:

Users browsing this thread: 1 Guest(s)