添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
谦虚好学的柚子  ·  Arduino Lab Tutorial: ...·  5 天前    · 
健壮的皮带  ·  python DataFrame循环读取 ...·  5 天前    · 
健身的键盘  ·  Python Docker IMAGE 的挑选·  4 天前    · 
强悍的梨子  ·  python ...·  昨天    · 
高大的海龟  ·  Reference list - APA ...·  5 月前    · 
豪情万千的松鼠  ·  little ...·  5 月前    · 
想发财的脸盆  ·  MA Games Art and ...·  6 月前    · 
Python模块和包
5 Python文件操作
5.1 Python文件的基本操作
5.2 Python open():打开文件
5.3 Python read()函数:读入指定长度的文本
5.4 Python readline()函数:读取一行文本
5.5 Python readlines()函数:读取所有行的文本
5.6 Python write()和writelines()函数:写入文本
5.7 Python tell()函数:获得当前读取位置
5.8 Python seek()函数:设定读写位置
5.9 Python fileno()函数:得到文件编号
5.10 Python closed()函数:关闭打开的文件
5.11 Python文件对象的属性
5.12 Python csv模块:处理csv文件
5.13 Python yaml模块:处理yaml文件
5.14 Python json模块:处理json文件
Python异常处理 Python类和对象 Python多线程和多进程 Python网络编程 Python Scrapy爬虫框架 Python Flask框架 Python操作Redis Python Tkinter库 Python wxPython库 Python PyQt5库 Python Matplotlib库 Python NumPy库
read() 函数从当前位置开始读,读出指定个数的字符。其返回值是一个字符串,表示读取的文件内容。
参数 size 如果为正数,表示最多读出 size 个字符;如果 size 为 0,则什么也不会读出,返回值是空字符串;如果 size 为负数,表示读出全部的内容。size 的默认值是 -1,表示读出全部的内容。
下面的例子演示了不指定 size 的值而使用默认值 -1 的情况。 >>> fd = open("in.dat", "r") # 以只读方式打开文件in.dat >>> ret_str = fd.read() # 将所有文件内容读到ret_str,size=-1 >>> type(ret_str) # 返回值类型是字符串 <class 'str'> >>> len(ret_str) # 字符串长度为68 >>> print(ret_str) # 显示文件内容 this is input text file it contains 3 lines this is the end of file >>> fd.close() # 关闭文件 下面演示指定 size,并且 size 为正数的情况。 >>> fd = open("in.dat", "r") # 以只读方式打开文件in.dat >>> str1 = fd.read(40) # 读入最多40个字符,保存到str1中 >>> str2 = fd.read(40) # 读入最多40个字符,保存到str2中 >>> len(str1) # str1包含40个字符 >>> len(str2) # str2包含28个字符,总共是68个字符 >>> print(str1+str2) # 将str1和str2连接起来,就是文件的完整内容 this is input text file it contains 3 lines this is the end of file >>> fd.close() # 关闭文件 如果到了文件的尾部,则返回空字符串。
>>> fd = open("in.dat", "r") # 以只读方式打开文件in.dat >>> str1 = fd.read() # 读出全部内容 >>> str2 = fd.read() # 这时已经到了文件的尾部 >>> type(str2) # 返回值类型是字符串 <class 'str'> >>> len(str2) # str2的长度为0,所以是空字符串 >>> fd.close() # 关闭文件
  • C语言打印杨辉三角
  • 嵌入式存储器概述
  • Linux初学者该选择哪个发行版?
  • Linux grep命令的用法
  • wxPython Button按钮的用法
  • Python PyQt5安装和使用
  • Spring Boot Actuator应用监控组件
  • Spring Boot Interceptor(拦截器)详解
  • Java throw和throws:声明和抛出异常
  • Spring Boot自动配置原理
  •