简体   繁体   English

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

[英]“IOError: [Errno 0] Error” error in Python

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]

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或倒带操作。

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. f.seek(0,2)2from_what参数。 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值为0从文件开头开始, 1使用当前文件位置, 2使用文件末尾作为参考点。 from_what can be omitted and defaults to 0 , using the beginning of the file as the reference point. from_what可以省略,默认为0 ,使用文件的开头作为参考点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IOError [Errno 2] Python中的Pickle错误 - IOError [Errno 2] Pickle error in python Python错误“ IOError:[Errno 2]没有这样的文件或目录”,但文件在那里 - Python error “IOError: [Errno 2] No such file or directory” but file is there Python urlopen IOError:[Errno socket错误] [Errno 10060] - Python urlopen IOError: [Errno socket error] [Errno 10060] IOError:[Errno 5]输入/输出错误 - IOError: [Errno 5] Input/output error 在 python 中打开文件会出现错误 IOError: [Errno 2] No such file or directory: &#39;&#39; on MAC - Opening a file in python gives the error IOError: [Errno 2] No such file or directory: '' on MAc Python:IOError:[Errno 13]权限被拒绝错误? 隐私设置? - Python: IOError: [Errno 13] Permission denied error? Privacy setting? Python:错误IOError:[Errno2]文件或目录的错误解释 - Python: error IOError: [Errno2] bad interpretation of file or directory 获取“IOError:[Errno 2]没有这样的文件或目录:'myoutfile.csv'”错误在Python中 - Getting “IOError: [Errno 2] No such file or directory: 'myoutfile.csv' ” error in Python IOError:[Errno套接字错误]使用BeautifulSoup - IOError: [Errno socket error] using BeautifulSoup IOError:[Errno套接字错误] [Errno 8]提供的节点名或服务名,或者未知 - IOError: [Errno socket error] [Errno 8] nodename nor servname provided, or not known
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM