美文网首页
python 指定位置操作文件

python 指定位置操作文件

作者: shawna_god | 来源:发表于2018-05-31 12:32 被阅读0次

    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

    相关文章

      网友评论

          本文标题:python 指定位置操作文件

          本文链接:https://www.haomeiwen.com/subject/wzlqsftx.html