Home > Backend Development > Python Tutorial > 简单文件操作python 修改文件指定行的方法

简单文件操作python 修改文件指定行的方法

WBOY
Release: 2016-06-16 08:46:32
Original
1445 people have browsed it

    例一:

复制代码 代码如下:

#!/usr/bin/python
import sys
import re
if __name__=="__main__":
 f=file("hi.txt","w+")
 li=["hello\n","hi\n"]
 f.writelines(li)
 f.close()

    "W+"模式:如果没有hi.txt则创建文件写入;如果存在,则清空hi.txt内容,从新写入。

 例二:修改文件指定行

    用的方法比拟笨,将文件内容按行读入到一个列表中,修改指定行即给列表中元素赋值;修改完后,用writelines将列表从新写入文件。

   

复制代码 代码如下:

#!/usr/bin/python

import sys,os

f=open('hi.txt','r+')
flist=f.readlines()
flist[4]='hi\n'
f=open('hi.txt','w+')
f.writelines(flist)


    将hi.txt第五行内容修改成hi
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template