Python天天美味(18) - linecache.getline()读取文件中特定一行

本文介绍如何使用Python标准库linecache中的getline方法来读取文件中的特定行。通过实例演示了如何从指定文件读取第4行的内容,并展示了该方法的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python中使用标准库中的linecache中的getline方法可以从某个文件中读取出特定的一行。比如:
import  linecache
print  linecache.getline( ' 2.1_open.py ' 4 )

将返回我上一节事例代码文件2.1_open.py的第4行文字,输出结果:
f = open('/home/evergreen/桌面/test')

查看linecache中的实现(我用的是Ulipad,所以直接将光标停留在linecache处,按F6键)

linecache.py

 

Python 天天美味系列(总)

Python 天天美味(16) - 过滤字符串的技巧,map与itertools.imap  

Python 天天美味(17) - open读写文件  

Python 天天美味(18) - linecache.getline()读取文件中特定一行  

Python 天天美味(19) - 时间处理datetime  

Python 天天美味(20) - 命令行参数sys.argv   

...
0.80s - Error in linecache.getline('C:\\Users\\cz\\Desktop\\期末大作业\\代码\\PythonApplication1\\PythonApplication1.py', 2, f_globals) Traceback (most recent call last): File "_pydevd_bundle\\pydevd_cython.pyx", line 1580, in _pydevd_bundle.pydevd_cython.handle_exception File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\linecache.py", line 30, in getline lines = getlines(filename, module_globals) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\linecache.py", line 46, in getlines return updatecache(filename, module_globals) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\linecache.py", line 137, in updatecache lines = fp.readlines() File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbe in position 109: invalid start byte 0.00s - Error on build_exception_info_response. Traceback (most recent call last): File "c:\users\cz\desktop\期末大作业\新建文件夹\common7\ide\extensions\microsoft\python\core\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 1508, in build_exception_info_response line_text = linecache.getline(original_filename, lineno) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\linecache.py", line 30, in getline lines = getlines(filename, module_globals) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\linecache.py", line 46, in getlines return updatecache(filename, module_globals) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\linecache.py", line 137, in updatecache lines = fp.readlines() File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbe in position 109: invalid start byte 0.01s - Error on build_exception_info_response. Traceback (most recent call last): File "c:\users\cz\desktop\期末大作业\新建文件夹\common7\ide\extensions\microsoft\python\core\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 1508, in build_exception_info_response line_text = linecache.getline(original_filename, lineno) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\linecache.py", line 30, in getline lines = getlines(filename, module_globals) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\linecache.py", line 46, in getlines return updatecache(filename, module_globals) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\linecache.py", line 137, in updatecache lines = fp.readlines() File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbe in position 109: invalid start byte
最新发布
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值