>>> #write file
>>> f = open('D:/temp/python_test.txt', 'w')
>>> f.write('abcdefghijklmnopqrstuvwxyz\r\n')
>>> f.write('0123456789\r\n') #\r\n is line separator on Windows
>>> val = ('value is', 12)
>>> f.write(str(val)) # nonstring object should be convert string first
>>> f.close()
>>> #read file
>>> f = open('D:/temp/python_test.txt', 'r')
>>> f.read()
"abcdefghijklmnopqrstuvwxyz\r\n0123456789\r\n('value is', 12)"
>>> f.seek(0, 0)
>>> f.tell()
0L
>>> f.readline()
'abcdefghijklmnopqrstuvwxyz\r\n'
>>> f.readlines()
['0123456789\r\n', "('value is', 12)"]
>>> f.seek(0, 0) # move to begin of the file
>>> f.read(3)
'abc'
>>> f.seek(4, 1) # skip 4 bytes
>>> f.read(1)
'h'
>>> f.seek(-2, 2) # Go to the 3rd byte before the end
>>> f.read(1)
'2'
>>> f.seek(0, 0)
>>>
>>> # read line by line
>>> for line in f :
print line
abcdefghijklmnopqrstuvwxyz
0123456789
('value is', 12)
>>> f.close()
>>> # read with 'with'
>>> with open('D:/temp/python_test.txt', 'r') as f:
for line in f :
print line
abcdefghijklmnopqrstuvwxyz
0123456789
('value is', 12)
>>> # serialize/unserialize object
>>> with open('D:/temp/python_test.txt', 'w') as f :
pickle.dump([1, '33', 3.14], f)
>>> with open('D:/temp/python_test.txt', 'r') as f :
x = pickle.load(f)
print x
[1, '33', 3.14]