Skip to content Skip to sidebar Skip to footer

How To Write To A Text File Using Python ?

I am trying to output a full for iteration. The output should be in a text file. How should I code for that ? The output should look like : Iteration 1 values --------> val1 &l

Solution 1:

numbers = [1,2,3,4,5,6]

with open('output.txt', 'w') as f:
  f.write('\t'.join(numbers))

Not really sure what else you mean


Solution 2:

file = open(filename,"w")
file.write("\t This is tabbed in \n This is a new line")
file.close()

Read here about file handling in python
http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files


Post a Comment for "How To Write To A Text File Using Python ?"