In this python file program, we will write a tuple to a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.
Line1 : This is India.
Line2 : This is America.
Line3 : This is Canada.
Line4 : This is Australia.
Line5 : This is Africa.
Line6 : This is Korea.
Line7 : This is Germany.
Line8 : This is China.
Steps to solve the program
- Create a tuple.
- Open the file by using open(“readcontent.txt”,”w”).
- Where file name is the name of the file and w is writing mode.
- Inside write() use ” “.join(tup) to write the tuple in the file.
- Close the file.
# Create a tuple
tup = ('1','2','3')
# Open file in write mode
f = open('readcontent.txt','w')
# Write tuple to file
f.write(" ".join(tup))
# Close the file
f.close()
Output : Open the readcontent.txt file to see the tuple.
1 2 3