regex – IOError: [Errno 0] Error error in Python
regex – IOError: [Errno 0] Error error in Python
Quoting answer from Python file operations, when switching between reading and writing on Windows, there must be an intervening fflush, fsetpos, fseek, or rewind operation.
Here is a possible fix:
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.seek(0,2) # change is here !!
f.write(n + num_words)
else:
print nNo match found in 2 words.txt file
append_2synonym(words_list, num_words)
In f.seek(0,2)
, 2
is the from_what
argument. A from_what
value of 0
measures from the beginning of the file, 1
uses the current file position, and 2
uses the end of the file as the reference point. from_what
can be omitted and defaults to 0
, using the beginning of the file as the reference point.