您的位置:首页 > 编程语言 > Python开发

Python读写文件及文件指针定位

2009-08-07 20:19 369 查看
读文件用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这个包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: