Python file program to write a tuple to a file

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
  1. Create a tuple.
  2. Open the file by using open(“readcontent.txt”,”w”).
  3. Where file name is the name of the file and w is writing mode.
  4. Inside write() use ” “.join(tup) to write the tuple in the file.
  5. 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

 

 

get the file size of a file.

check whether a file is closed or not.

Leave a Comment