8

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("\n" + num_words)
        else:
            print "\nNo 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 Briefcase\My Python Scripts\Spin Scripts\synonyms\testing2.py", line 16, in <module>
    append_2synonym(words_list, num_words)
  File "D:\Magic Briefcase\My Python Scripts\Spin Scripts\synonyms\testing2.py", line 12, in append_2synonym
    f.write("\n" + num_words)
IOError: [Errno 0] Error
[Finished in 0.1s with exit code 1]
2

1 Answer 1

9

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.

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

4 Comments

Since f.read() reads to EOF, the original should work: the C standard allows switching from read to write mode if read encounters EOF. If this does fix it, that implies the C library on (some compilers and/or some versions of?) Windows gets this wrong.
@btoueg - thanks this is working. I tried it several times and the apeending keeps working and growing the 'test2 words.txt' file. I just tried looking at the python docs about .seek(), but I could not totally figure out f.seek(0,2). I understand that the first argument 0 effectively rewinds to the beginning of the file, but what is the 2 doing?
I've edited the answer. For more details, go check the answer I am referring to.
Thank you. I will now study that answer in more details. I appreciate the help :-)

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.