-1

I have a file with this format:

  1. Frank,456,768,987
  2. Mike,123,456,798

And I'm using this code:

name = input()
age = float(input())    
ident = float(input())   
phone = float(input())    
f = open("Test.txt","r")
lines = f.readlines()
f.close()
f = open("test.txt", "w")
data = [name, age, ident, phone]
for line in lines:
    if line!= data:           
        f.write(line)

So, if the list with the inputs equals a line, that line must be removed. Why is this code not working? The files becomes empty.

styvane
  • 59,869
  • 19
  • 150
  • 156
  • First of all you don't need to convert you *input* value to `float` because file content are string. Also you should use the [`csv`](https://docs.python.org/3/library/csv.html) module – styvane Mar 25 '16 at 20:21

5 Answers5

0

You are writing the data into a buffer and you need to flush the text from the buffer to the file and then don't forget to close it.

This should be helpful..

what exactly the python's file.flush() is doing?

Hope it's worked:)

Community
  • 1
  • 1
0

This should work:

f=open("Test.txt","r")
fw=open("test.txt","w")
data= name+","+str(age)+","+str(ident)+","+str(phone)
for line in f:
   if not(data in line):
      fw.write(line)
f.close()
fw.close()
Denis
  • 1,219
  • 1
  • 10
  • 15
0

The problem is that line is a string, and data is a list. Also, do not convert to float because you wil have to re-convert to string. Finally, this will also fail because of end-of-line characters at the end of each line.

name=input()
age=input()
ident=input()
phone=input()

with open("Test.txt","r") as f:
    lines=f.readlines()
with open("test.txt","w") as f:
  data=[name,age,ident,phone]
  for line in lines:
    if any(l!=d for l,d in zip(line.strip('\n\r').split(','),data)):
      f.write(line)

This solution compares two lists, item by item. Another solution would be to build a unique string with ",".join(data) (see other people's answers).

Sci Prog
  • 2,651
  • 1
  • 10
  • 18
0

One of the problems is that you are comparing a string with a list. The other problem is that you are not closing the file at the end. Here is a version that works:

name = "Frank"
age = 456
ident = 768
phone = 987

f=open("Test.txt","r")
lines=f.readlines()
f.close()

data=",".join(map(str, [name,age,ident,phone]))
with open("test.txt", "w+") as x:
    for line in lines:
        if line.strip() != data:
            x.write(line)

I just hardcoded the values at the beginning for the sake of simplicity. I'm assuming that the file will always have the same correct format. You could also use regex and do some pattern matching. Here I'm making sure that I am converting all the values to string, since join won't accept any integer:

data=",".join(map(str, [name,age,ident,phone]))
Pedro Rodrigues
  • 424
  • 5
  • 16
0

The following code snippet is working properly.

name=input()
age=input()
ident=input()
phone=input()

f=open("Test.txt","r")
fw=open("TestResult.txt","w")
data= name+","+str(age)+","+str(ident)+","+str(phone)
for line in f:
  if not(data in line):
  fw.write(line)
f.close()
fw.close()

Test.txt input

shovon,23,1628,017
shovo,24,1628,017
shov,25,1628,017
sho,26,1628,017

TestResult.txt output

shovo,24,1628,017
shov,25,1628,017
sho,26,1628,017

arshovon
  • 13,270
  • 9
  • 51
  • 69