In [1]: f=open('abc.txt','r+') #打开文件,读写模式
In [3]: f.tell() #查看当前指针
Out[3]: 0
In [4]: f.read() #读取文件,默认全部读取
Out[4]: "Beautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\nFlat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.\nAlthough practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.\nThere should be one-- and preferably only one --obvious way to do it.\nAlthough that way may not be obvious at first unless you're Dutch.\nNow is better than never.\nAlthough never is often better than *right* now.\nIf the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea.\nNamespaces are one honking great idea -- let's do more of those!\n"
In [5]: f.tell() #查看当前指针
Out[5]: 823
In [6]: f.seek(0) #将指针移到开始处
Out[6]: 0
In [7]: f.readline() #读取单行文件
Out[7]: 'Beautiful is better than ugly.\n'
In [11]: f.readlines() #读取所有行返回列表
Out[11]:
['Flat is better than nested.\n',
'Sparse is better than dense.\n',
'Readability counts.\n',
"Special cases aren't special enough to break the rules.\n",
'Although practicality beats purity.\n',
'Errors should never pass silently.\n',
'Unless explicitly silenced.\n',
'In the face of ambiguity, refuse the temptation to guess.\n',
'There should be one-- and preferably only one --obvious way to do it.\n',
"Although that way may not be obvious at first unless you're Dutch.\n",
'Now is better than never.\n',
'Although never is often better than *right* now.\n',
"If the implementation is hard to explain, it's a bad idea.\n",
'If the implementation is easy to explain, it may be a good idea.\n',
"Namespaces are one honking great idea -- let's do more of those!\n"]
In [33]: f.seek(0)
Out[33]: 0
In [34]: f.write('ksfdhauy3urnncb') #写入字符串
Out[34]: 15
In [35]: f.close() #关闭文件
In [36]: f=open('abc.txt','r+') #再次打开文件查看
In [37]: f.readline()
Out[37]: 'ksfdhauy3urnncbtter than ugly.\n' #以读写模式打开时写入的文件灰覆盖已有的
In [39]: f=open('abc.txt','a') #追加模式打开文件
In [40]: f.tell() #偏移指针直接在末尾处
Out[40]: 835
In [41]: f.write('5671528956knkxb') #写入文件
Out[41]: 15
In [47]: f.read() #查看文件,追加字符卸载了文件末尾处
Out[47]: "btter than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\nFlat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.\nAlthough practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.\nThere should be one-- and preferably only one --obvious way to do it.\nAlthough that way may not be obvious at first unless you're Dutch.\nNow is better than never.\nAlthough never is often better than *right* now.\nIf the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea.\nNamespaces are one honking great idea -- let's do more of those!\nhello world\n5671528956knkxb"