python中文件读取read、指针位置tell、移动指针seek函数
001、文件对象read读入文件
>>> in_file = open("a.txt", "r") >>> in_file.read() ## 'abcd\nefgh\ni\n'
002、文件对象tell 返回指针再文件中的位置
>>> in_file = open("a.txt", "r") ## 打开文件 >>> in_file.tell() ## 返回文件指针当前的位置 0 >>> in_file.read() ## 读入文件 'abcd\nefgh\ni\n' >>> in_file.tell() ## 返回指针当前的位置 12
003、文件对象seek移动指针
>>> in_file = open("a.txt", "r") ## 打开文件 >>> in_file.tell() ## 返回当前指针 0 >>> in_file.read() ## 读入文件 'abcd\nefgh\ni\n' >>> in_file.tell() ## 返回当前指针位置 12 >>> in_file.seek(5,0) ## 从0开始偏移5 5 >>> in_file.tell() ## 返回当前指针位置 5 >>> in_file.read() ## 从指针位置读入文件 'efgh\ni\n'
004、设定读入的字符数目
>>> in_file = open("a.txt", "r") ## 打开文件 >>> in_file.read(5) ## 读入5个字符 'abcd\n' >>> in_file.tell() ## 返回当前指针的位置 5
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2021-07-03 2.3.4 设A为n阶方阵,满足AA^T = I, |A| < 0, 求|A + I|.