读文件用read,传递要读的字节数 写文件用write,传递要写的字符串 1. 写文件 >>> f = open('e://test.txt','w') >>> a = [1, 2] >>> f.write(a); Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> f.write(a); TypeError: write() argument 1 must be str, not list >>> f.write('hello world'); 11 >>> f.close() >>> 2. 读文件 >>> f = open('e://test.txt', 'r') >>> f.read(5) 'hello' >>> f.seek(0, 1) 5 >>> f.read() ' world' >>> f.seek(0, 0) 0 >>> f.read() 'hello world' >>> f.close() >>> write只能写字符串,要写其他类型的数据,得用pickle这个包