1

i want to edit email id and phone number by taking user input but i am unable to that through this what i am doing wrong in this code help.

file.txt

Jon Snow 996452544 Jon@gmail
Robb 885546694 Robb@gmail
Robert 896756885 Robert@gmail

code

 def editContact():
    obj2 = open("address.txt","r")  
    output = []

    old_email=raw_input("Enter old email address : ")               
    new_email=raw_input("Enter new email address : ")
    s = re.sub(old_email, new_email, obj2)     

    obj1 = open("address.txt","w")
    obj1.writelines(s)
5
  • Please correct your indentation. Commented Apr 19, 2018 at 7:21
  • no issue in indentation code is working Traceback (most recent call last): File "./addressBookpy.py", line 72, in <module> editContact() File "./addressBookpy.py", line 54, in editContact s = re.sub(old_email, new_email, obj2) File "/usr/lib64/python2.6/re.py", line 151, in sub return _compile(pattern, 0).sub(repl, string, count) TypeError: expected string or buffer This error i am geting Commented Apr 19, 2018 at 7:25
  • The indentation issue is trying to read the code and answer your question. Show the error in your question, not in a comment. Commented Apr 19, 2018 at 7:26
  • 1
    Your error is because the third parameter of re.sub should be a string not a file object. Commented Apr 19, 2018 at 7:29
  • Note that opening a file with write access will truncate the file to zero bytes destroying existing data. Commented Apr 19, 2018 at 7:33

2 Answers 2

1

I think the issue you are having is :

s = re.sub(old_email, new_email, obj2)

obj2 here is a file obj, read the file using obj2.read()

then you will be able to replace.

Sign up to request clarification or add additional context in comments.

Comments

1
abc=obj2.read()
s = re.sub(old_email, new_email, abc) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.