tnfh.net
当前位置:首页 >> python 怎样或读取一个文件的最后一行 >>

python 怎样或读取一个文件的最后一行

a=open("a.txt","rb").readlines() for line in a: if(line.find("xxx") != -1): print "OK" break else: print "NG"

#!/usr/bin/env pythonimport osimport sysdef get_last_n_lines(logfile, n): blk_size_max = 4096 n_lines = [] with open(logfile, 'rb') as fp: fp.seek(0, os.SEEK_END) cur_pos = fp.tell() while cur_pos > 0 and len(n_lines) < n: blk_...

你这里有2个问题 1. 用csvfile.readlines() 2.你取的targetLine是一个字符串,需要进行分割 具体代码如下,测试通过,请采纳^_^ # coding=utf8 with open("C:\\test.csv") as csvfile: mLines = csvfile.readlines() targetLine = mLines[-1] a=...

fp = open("file") for i, line in enumerate(fp): if i == 25: # 26th line elif i == 29: # 30th line elif i > 29: break fp.close() 或者: import linecache file=open('3_2.txt','r') linecount=len(file.readlines()) linecache.getline(...

while True: m = raw_input("input: ") if m: with open("test.txt", "aw") as f: f.write(m + "\n") continue break

python读取文件内容的方法: 一.最方便的方法是一次性读取文件中的所有内容并放置到一个大字符串中: all_the_text = open('thefile.txt').read( ) # 文本文件中的所有文本 all_the_data = open('abinfile','rb').read( ) # 二进制文件中的所有...

把你的代码改一下 f = open("test.txt", "r") while True: line = f.readline() if line: pass # do something here line=line.strip() p=line.rfind('.') filename=line[0:p] print "create %s"%line else: breakf.close()写得可能罗嗦些,就是...

#!/usr/bin/env python f = open('file.txt','a') f.write('I have a pen') f.close()

import osfilepath='/usr/bin'files=os.listdir(filepath) #获取filepath路径下的所有文件列表filename='aa.txt'fullname=(os.sep).join([filepath,filename])with open(fullname) as f: s=f.read()

网站首页 | 网站地图
All rights reserved Powered by www.tnfh.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com