Storing and parsing Python objects in files : Text file « File « Python Tutorial






X, Y, Z = 43, 44, 45                      
S = 'Spam'                                
D = {'a': 1, 'b': 2} 
L = [1, 2, 3] 

F = open('datafile.txt', 'w')             
F.write(S + '\n')                         
F.write('%s,%s,%s\n' % (X, Y, Z))         
F.write(str(L) + '$' + str(D) + '\n')     
F.close(  ) 

bytes = open('datafile.txt').read(  )     
print bytes 


X, Y, Z = 43, 44, 45                      
S = 'Spam'                                
D = {'a': 1, 'b': 2} 
L = [1, 2, 3] 

F = open('datafile.txt', 'w')             
F.write(S + '\n')                         
F.write('%s,%s,%s\n' % (X, Y, Z))          
F.write(str(L) + '$' + str(D) + '\n')      
F.close(  )








12.3.Text file
12.3.1.First Python Programs
12.3.2.File Read and Display
12.3.3.Demonstrates reading from a text file
12.3.4.Reads a plain text file
12.3.5.Writing to a text file
12.3.6.Open a file and save text to it
12.3.7.Storing and parsing Python objects in files
12.3.8.print to a file