Hello,am newbie in python.Am trying to write a function that deletes urls in a file.The function accepts the url to be deleted as an argument.I want to use regular expression to match the url in the file and then delete it(maybe replacing it with a white space).My problem is that i cant seem to get the regular expression right.
Here is the makeup of the file:
http:// www.google.com
http://www.digg.com
http://www.digg.com/signup
http://www.cnn.com
My Code:
import re
def delete_url(url):
file=open('myurls.txt','r+')
file_content=file.read()
p=re.compile('\b'+url+'\b') #Url re (maybe wrong)
new_content=p.sub(' ',url)
file.write(new_content) #Write the new string to file
Sorry if my code look too "noobish".Basically what i want is a function that deletes urls from a file based on the url passed to it.For example,
delete_url("http://www.digg.com")
"""This should delete http://www.digg.com and not some part of urls with paths like http://www.digg.com/people/sigup
Thanks in advance