>> s=f.read()#从文件指针所在的位置,读到文件结尾>>> s'hello python!\nhello world!\n'>>> f.close()2》>>> f=open(r..._python 文件 read()">
当前位置:   article > 正文

python中read函数解释_Python内置函数:read()

python 文件 read()

1》

>>> f=open(r"C:\Users\Administrator\Desktop\test.txt",'r')

>>> s=f.read()#从文件指针所在的位置,读到文件结尾

>>> s

'hello python!\nhello world!\n'

>>> f.close()

2》

>>> 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!

>>>

3》

>>> 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

''

>>>

(完)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/730887
推荐阅读
相关标签
  

闽ICP备14008679号