#! /usr/bin/python
fp = file('data.txt') #指定文件
s = fp.read() #将指定文件读入内存
fp.close() #关闭该文件
a = s.split('\n')
a.insert(LINE, 'a new line') #在第 LINE+1 行插入
s = '\n'.join(a) #用'\n'连接各个元素
fp = file('data.txt', 'w')
fp.write(s)
fp.close()
fp = file('data.txt')
lines = []
for line in fp: #内置的迭代器, 效率很高
lines.append(line)
fp.close()
lines.insert(LINE, 'a new line') #在第 LINE+1 行插入
s = '\n'.join(lines)
fp = file('data.txt', 'w')
fp.write(s)
fp.close()
div>
Read More:
- C ා use math.net Read. Mat file, file content is complex
- In Python, print() prints to remove line breaks
- Three ways of single line and multi line comment in Python
- Insert keyframe giving keying set failed to insert any key frames (solved)
- Insert keyframe giving keying set failed to insert any key frames
- Pre initialization of list content and length in Python
- Java 8 Stream – Read a file line by line
- Python implements inserting content in the specified position of text
- Python uses CX_ Oracle batch insert error report ora-01036 error solution
- How to solve oserror: [errno 98] address already in use and kill the python3 process
- The python version output from the command line is inconsistent with the python version in the current CONDA environment
- Because the computer is stuck, force to restart the computer and open idea idea to report an error on line 1: no content is allowed in the foreword.
- How to use Python split() function (split array)
- XML file should have root tag (extra content at the end of the document in file error)
- Typeerror: write() argument must be STR, not bytes and the method of writing binary file in Python 3
- Typeerror: write() argument must be STR, not bytes and the method of writing binary file in Python 3
- Syntax error, insert “Finally” to complete TryStatem in Myeclipse in Java-
- The language of C__ FILE__ 、__ LINE__ And line
- Use subprocess to execute the command line, and the pipeline is blocked
- [Solved] VUE Error: “You may use special comments to disable some warnings.Use // eslint-disable-next-line to …