question 183 Files & Directories · medium
Add a subscriber only once
A newsletter keeps its subscribers in a text file, one email address per line. Sign-ups arrive with stray spaces and capital letters, and people sign up twice. Write a function that adds an address only if it isn't already in the file.
Function
add_subscriber(path, email)Compareaddresses are compared after
strip() and lower(), on both sidesEffectwhen new, the cleaned address (stripped, lower case) is added on its own line at the end; a missing file is created
Returns
True if the address was added, False if it was already thereRulesevery existing line ends with a newline; blank lines in the file are ignored
Examples
calladd_subscriber('subscribers.txt', 'SAMUEL_MENSAH@yahoo.com')returnsFalse
calladd_subscriber('fresh.txt', 'mei.adams48@gmail.com')returnsTrue
calladd_subscriber('subscribers2.txt', ' Aisha.Silva@gmail.com ')returnsTrue