一 文件读写
>>> f=open(r"F:\pytest\1.txt","w") >>> f.write("Python 是一种非常好的语言。\n我喜欢Python!!\n") 30 >>> f.close()
>>> f=open(r"F:\pytest\1.txt","w") >>> str = f.readline() Traceback (most recent call last): File "<stdin>", line 1, in <module> io.UnsupportedOperation: not readable >>> f=open(r"F:\pytest\1.txt","r") >>> str=f.readline() >>> print(str)
>>> f=open(r"F:\pytest\1.txt","r") >>> a=f.readlines() >>> print(a) ['safasfdasfdasfda afasdfas发顺丰三法师打发啊啊发顺丰啊手动阀 \n', ' 啊发顺丰大手动阀发 \n'] >>> f.close()
>>> f=open(r"F:\pytest\1.txt","rb+")
>>> f.write(b"12123123123131231")
17
>>> f.tell() #指针位置,从开头计算的字节数
17
>>> f.seek(5) #从头移动5个字符,位数默认0
5
>>> f.tell()
5
>>> f.read(1)
b'1'
>>> f.seek(-3,2) #从尾前移3个字符;1代表从当前位置后移字符数
81
>>> f.read(1)
b' '
浙公网安备 33010602011771号