从文件中删除行

时间:2020-01-13 16:42:48

标签: python python-2.7

我有一个文件:

a;b;c
d;x;f
g;h;z

我需要从包含x的文件中删除行(结果包含第一行和最后一行)。如何实现呢?

代码逻辑正在使用追加模式,并以读取模式检测其他文件。这是否意味着我必须在检测到时切换open模式,将其关闭然后再次打开追加更多内容?

我可以添加到文件中的代码

f1 = open(file1, 'a')
f2 = open(file2, 'r'):
    line = ''
    (reading logic..)
    if 'x' in line:
        # need here to remove the line

还是没有使用open的其他方法?我正在使用Python 2.6.6。

0 个答案:

没有答案