-5
imp,2,6,7

ads,4,5,6

sfd,2,5,8

I have a text file that looks like this I want to delete the line that has imp in it.

All the other methods I have seen to delete lines from files only work for single strings

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • And what have you tried so far? If you are stuck with some code we can help you? SO is not a code-writing or tutorial service – Anand S Kumar Jul 01 '15 at 08:30
  • I've tried something like this http://stackoverflow.com/questions/4710067/deleting-a-specific-line-in-a-file-python – James Redfearn Jul 01 '15 at 08:31
  • Please post the code of what you have tried so far. Posting links won't do anything for you. The easier you make for others to understand the problem and your code the better help you will get. – bane19 Jul 01 '15 at 08:34
  • what have you tried so far? have your tried `string.find()` or `re.match()`? – Will Jul 01 '15 at 08:36
  • @JamesRedfearn The only thing you need to do different from the answer in the other thread are to use "'imp' not in line" as the condition to not write the line back. – Binnut Jul 01 '15 at 08:40
  • There is no `tuple`. There is just something that looks like the _representation_ of a `tuple`. – Matthias Jul 01 '15 at 08:46

1 Answers1

1

Following this question link, you can try this:

fn = 'Test.txt'
f = open(fn)
output = []
for line in f:
    if not "imp" in line:
        output.append(line)
f.close()
f = open(fn, 'w')
f.writelines(output)
f.close()

Result:

ads,4,5,6

sfd,2,5,8

Community
  • 1
  • 1
GAVD
  • 1,977
  • 3
  • 22
  • 40