python 指定位置操作文件

Python 中open可以操作文件,在文件操作时 需要找到其中某关键字,然后进行插入 、替换或删除等操作;

所以需要 定位文件指针,seek定位操作位置等;且不需要生成新的附件文件,可操作较大的文件;

1. 指定位置插入数据

def add_host(file_name, host, group):

print file_name, host, group

res= {

'status':True,

        'message':''

    }

file_name ='./deploy/hosts' if not file_nameelse file_name

try:

i =1

        with open(file_name, 'r')as f:

with open(file_name, 'r+')as f_w:

line = f.readline()

while line:

if groupin line:

f_w.seek(f.tell(), 0)

host = host +"\n"

                        f_w.write(host)

next_line = f.readline()

while next_line:

f_w.write(next_line)

next_line = f.readline()

f_w.truncate()

break

                    line = f.readline()

i +=1

    except IOError:

res['status'] =False

        res['message'] ='file wirte false!'

    return res


2. 指定位置删除数据

def del_host(file_name, host, group):

res= {

'status':True,

        'message':''

    }

file_name ='./deploy/hosts' if not file_nameelse file_name

try:

i =1

        with open(file_name, 'r')as f:

line = f.readline()

# print f.tell()

            while line:

if groupin line:

_this = f.tell()

break

                line = f.readline()

i +=1

            else:

_this =False

        i = i +1

        with open(file_name, 'r')as f_r:

lines = f_r.readlines()

f_r.seek(_this, 0)

line = f_r.readline()

while line:

if "[" in line:

_end = f_r.tell()

break

                if i ==len(lines):

print i, lines

_end = f_r.tell()

print 'last line', _end

break

                line = f_r.readline()

i +=1

            else:

_end =False

        # print 'start, end', _this, _end

        if _this ==False:

res['status'] =False

            res['message'] ='start  false!'

        elif _end ==False:

res['status'] =False

            res['message'] ='end  false!'

        elif _this == _end:

res['status'] =False

            res['message'] ='_this == _end!'

        else:

print 'start, end', _this, _end

try:

with open(file_name, 'r')as f_r:

with open(file_name, 'r+')as f_w:

f_r.seek(_this, 0)

line = f_r.readline()

host_list = []

while lineand f_r.tell() <= _end:

host ="192.168.100.44" if not hostelse host

print(line, f_r.tell())

host_list.append(f_r.tell())

if line.strip().replace('/n', '') == host:

_host = f_r.tell()

print f_r.tell()

for iin host_list:

if i == _host:

now_location = host_list[host_list.index(i) -1]

print 'now_location', now_location

print 'host it..', line, i

f_w.seek(now_location, 0)

# f_r.readline()

                                print 'i', i, line

next_line = f_r.readline()

print 'i--', i, next_line

# next_line = line

                                while next_line:

# print 'next line', next_line

                                    f_w.write(next_line)

next_line = f_r.readline()

# break

                                f_w.truncate()

line = f_r.readline()

except Exception as e:

res['status'] =False

                res['msg'] = e

except Exception as e:

res['status'] =False

        res['msg'] = e

return res

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,481评论 0 5
  • 书读完之后让我想起之前没读完的<平凡的日子>, 题材挺像, 叙事手法也差不多, 但更积极更好看更宽容; 作者的文笔...
    Niiing阅读 582评论 0 1
  • 独钓云烟阅读 476评论 10 20
  • 时间一刻不停歇,此刻已是2018年,2017年想干的3件事完成了2件。其实第三件换工作的事也算完成了,只不过去的不...
    心灵深处ye阅读 200评论 0 0
  • 问题和现状 产品会在临上线一两天提出需求变更 在需求评审阶段,看不出问题,在执行阶段,发现逻辑需要修改。 领导临时...
    松哥888阅读 3,464评论 0 5