惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
N
News | PayPal Newsroom
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
博客园 - Franky
SecWiki News
SecWiki News
Recent Announcements
Recent Announcements
T
Troy Hunt's Blog
The Register - Security
The Register - Security
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
S
Security Affairs
博客园 - 司徒正美
S
Schneier on Security
I
InfoQ
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Forbes - Security
Forbes - Security
腾讯CDC
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
V
Vulnerabilities – Threatpost
C
Check Point Blog
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
Schneier on Security
Schneier on Security
O
OpenAI News
K
Kaspersky official blog

博客园 - Danny Chen

python asyncio 获取协程返回值和使用callback c#等待所有子线程执行完毕方法 Python virtualenv 查看当前正在运行的python进程 Mac 删除/卸载 自己安装的python pycharm 注册码/License server 2017年最新 Linux cat命令详解 在Mac平台上安装配置ELK时的一些总结 Mac上搭建ELK C#中用schema验证xml的合法性 C#中XML与对象之间的序列化、反序列化 C#位运算 SQL Server 权限管理 如何用C#动态编译、执行代码 [C#]手把手教你打造Socket的TCP通讯连接(一) C#动态调用WCF接口,两种方式任你选。 动态调用WCF服务 矩阵的坐标变换(转) 【.NET线程--进阶(一)】--线程方法详解
python读取txt文件最后一行(文件大+文件小)
Danny Chen · 2017-12-28 · via 博客园 - Danny Chen

txt文件小

#coding:utf-8
'''
fname为所读xx.txt文件
输出为:文件第一行和最后一行
'''

fname = 'test.txt'
with open(fname, 'r') as f:  #打开文件
    lines = f.readlines() #读取所有行
    first_line = lines[0] #取第一行
    last_line = lines[-1] #取最后一行     
    print '文件' + fname + '第一行为:' + first_line
    print '文件' + fname + '最后一行为:'+ last_line

当文件很大时,采用这种方法不可行,资源浪费太大,采用下面方案。

txt文件大

#coding:utf-8
'''
f_name为所读xx.txt文件
输出为:文件最后一行
'''

fname = 'test.txt'
with open(fname, 'r') as f:  #打开文件
    first_line = f.readline()  #读第一行
    off = -50      #设置偏移量
    while True:
        f.seek(off, 2) #seek(off, 2)表示文件指针:从文件末尾(2)开始向前50个字符(-50)
        lines = f.readlines() #读取文件指针范围内所有行
        if len(lines)>=2: #判断是否最后至少有两行,这样保证了最后一行是完整的
            last_line = lines[-1] #取最后一行
            break
        #如果off为50时得到的readlines只有一行内容,那么不能保证最后一行是完整的
        #所以off翻倍重新运行,直到readlines不止一行
        off *= 2

    print '文件' + fname + '第一行为:' + first_line
    print '文件' + fname + '最后一行为:'+ last_line