How to write data to a file in Python?

0 votes
Hi. I have stored some data in a variable in Python. I want that data to be written in a text file. How can I do this?
May 12, 2019 in Python by Hiran
92 views

1 answer to this question.

0 votes

Refer to the below code.

data=’whatever your data is’
file=open('data.txt','w+')
file.write(data)
file.close()

Here, your data is stored in the variable ”data” and the name of the file you are storing it is “data.txt”

answered May 12, 2019 by Shaam

Related Questions In Python

0 votes
2 answers
0 votes
1 answer

How can I write code to find a palindrome in python without using string functions?

In Logic 1, try if i<int(length/2): instead of if i<int((length/2+1)): In ...READ MORE

answered Jan 16, 2019 in Python by charlie_brown
• 7,780 points
7,839 views
0 votes
1 answer

How to create and read from a temporary file in Python?

Hi, there is a very simple solution ...READ MORE

answered Jan 29, 2019 in Python by Nymeria
• 3,520 points
339 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,363 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
1,011 views
0 votes
1 answer

How to read data from a text file using Python?

Refer to the below example where the ...READ MORE

answered May 12, 2019 in Python by Sushma
325 views
0 votes
1 answer

How to print the index of a Pandas Dataframe?

You can do this using the code ...READ MORE

answered May 12, 2019 in Python by Usha
3,121 views