I have a script that should append something to a file, but it is raising an error that I don't understand and not sure how it is being triggered.
Here is the code:
import re
num_words = "four kiddiewinks|four children|four kids"
words_list = num_words.split('|')
def append_2synonym(words_list, num_words):
with open('test2 words.txt', 'a+') as f:
read_f = f.read()
patt = r'^' + words_list[0] + '|'
result = re.search(patt, read_f, re.MULTILINE)
if result == None:
f.write("
" + num_words)
else:
print "
No match found in '2 words.txt' file"
append_2synonym(words_list, num_words)
Here is the contents of the 'test2 words.txt' file:
five kiddiewinks|five kids|five children
mobile phone|cell phone|cellular phone
stinky cheese|smelly cheese
Here is the full error I am getting:
Traceback (most recent call last):
File "D:Magic BriefcaseMy Python ScriptsSpin Scriptssynonymsesting2.py", line 16, in <module>
append_2synonym(words_list, num_words)
File "D:Magic BriefcaseMy Python ScriptsSpin Scriptssynonymsesting2.py", line 12, in append_2synonym
f.write("
" + num_words)
IOError: [Errno 0] Error
[Finished in 0.1s with exit code 1]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…