Python內置函數:read()

文章轉載於:http://blog.csdn.net/sxingming/article/details/51337768(博主:快遞小哥)python

1》.net

>>> f=open(r"C:\Users\Administrator\Desktop\test.txt",'r')
>>> s=f.read()#從文件指針所在的位置,讀到文件結尾
>>> s
'hello python!\nhello world!\n'
>>> f.close()指針

2》blog

>>> f=open(r"C:\Users\Administrator\Desktop\test.txt",'r')
>>> s1=f.read(15)#從文件指針所在的位置開始讀15個字節
>>> s2=f.read()#從文件指針所在的位置讀到文件結尾
>>> f.close()
>>> s1
'hello python!\nh'
>>> s2
'ello world!\n'
>>> print s1,s2
hello python!
h ello world!字符串

>>> get

3》test

>>> f=open(r"C:\Users\Administrator\Desktop\test.txt",'r')
>>> s1=f.read()#讀取文件中全部的內容,返回一個字符串
>>> s2=f.read()#讀取到了0個字節,由於文件指針已經讀到文件尾部。
>>> f.close()
>>> s1
'hello python!\nhello world!\n'
>>> s2
''
>>> top

(完)文件

相關文章
相關標籤/搜索