Python short course: File operations basics in Python

 The file operation is important in Python development. Some basics operations are below:



In [1]: with open('spam.txt', 'w') as file:
   ...:     file.write(('spam'*5)+'\n')
   ...: 

In [2]: with open('spam.txt', 'r') as file:
   ...:     text = file.read()
   ...: 
   ...: 

In [3]: text
Out[3]: 'spamspamspamspamspam\n'

Comments