Deleting a line from a file in Python

后端 未结 5 2088
离开以前
离开以前 2020-12-05 20:27

I\'m trying to delete a specific line that contains a specific string.

I\'ve a file called numbers.txt with the following content:

5条回答
  •  心在旅途
    2020-12-05 21:17

    You can use regex.

    import re
    if not re.match("^tom$", line):
        output.append(line)
    

    The $ means the end of the string.

提交回复
热议问题