# python删除文件中某行

```python
fin=open('a.txt')
a=fin.readlines()
fout=open('newa.txt','w')
b=''.join(a[1:])
fout.write(b)
fin.close()
fout.close()
```

这个方法通过切片方式，可以去除掉指定行

> 参考 [python怎么删除txt文本里面的第一行？](http://zhidao.baidu.com/question/583049390.html)

```python
with open('file.txt', 'r') as fin:
    data = fin.read().splitlines(True)
with open('file.txt', 'w') as fout:
    fout.writelines(data[1:])
```

> 参考 [How to delete the first line of a text file using Python?](http://stackoverflow.com/questions/20364396/how-to-delete-the-first-line-of-a-text-file-using-python)，这个转换需要确保内存足够容纳文件内容

* 巨大文件的行删除

```python
def removeLine(filename, lineno):
    fro = open(filename, "rb")

    current_line = 0
    while current_line < lineno:
        fro.readline()
        current_line += 1

    seekpoint = fro.tell()
    frw = open(filename, "r+b")
    frw.seek(seekpoint, 0)

    # read the line we want to discard
    fro.readline()

    # now move the rest of the lines in the file 
    # one line back 
    chars = fro.readline()
    while chars:
        frw.writelines(chars)
        chars = fro.readline()

    fro.close()
    frw.truncate()
    frw.close()
```

> 参考 [Fastest Way to Delete a Line from Large File in Python](http://stackoverflow.com/questions/2329417/fastest-way-to-delete-a-line-from-large-file-in-python)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/python/startup/delete_line_from_file.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
