Python 中的“IOError: [Errno 0] Error”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19283118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:19:28  来源:igfitidea点击:

"IOError: [Errno 0] Error" error in Python

pythonregexruntime-error

提问by Darren Haynes

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:

这是“test2 words.txt”文件的内容:

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]

采纳答案by Benjamin Toueg

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.

引用Python 文件操作的答案,在 Windows 上进行读写切换时,必须有中间的 fflush、fsetpos、fseek 或 rewind 操作。

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), 2is the from_whatargument. A from_whatvalue of 0measures from the beginning of the file, 1uses the current file position, and 2uses the end of the file as the reference point. from_whatcan be omitted and defaults to 0, using the beginning of the file as the reference point.

f.seek(0,2),2from_what参数。从文件开头开始度量的from_what01使用当前文件位置,并2使用文件结尾作为参考点。from_what可以省略并默认为0,使用文件的开头作为参考点。