首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么会显示io.UnsupportedOperation:不可写

为什么会显示io.UnsupportedOperation:不可写
EN

Stack Overflow用户
提问于 2021-03-01 05:56:56
回答 1查看 43关注 0票数 2

导入pygame,随机

def quiteGame():

代码语言:javascript
复制
readFile = open("BestScore.txt")
bestScoreFile = readFile.read()
readFile.close()
writeFile = open("BestScore.txt")
iScore = max(score,int(bestScoreFile))
print('Your Score is :', score)
print('Highest Score is :', iScore)
writeFile.write(str(iScore))
writeFile.close()
pygame.quit()

为什么会有消息“writeFile.write(str(iScore)) io.UnsupportedOperation: not writable”显示在那里。

EN

回答 1

Stack Overflow用户

发布于 2021-03-01 15:01:15

为了写入文件,你需要在“可写”模式下打开它。默认情况下,python以“只读”模式打开任何.txt文件

要以可写模式打开文件,请执行以下操作:

代码语言:javascript
复制
writeFile = open("BestScore.txt","w") #Opens BestScore.txt in writable mode
[...]#Other code

因此,最终的代码应该如下所示:

代码语言:javascript
复制
import pygame, random

def quiteGame():

    readFile = open("BestScore.txt")
    bestScoreFile = readFile.read()
    readFile.close()
    writeFile = open("BestScore.txt","w")
    iScore = max(score,int(bestScoreFile))
    print('Your Score is :', score)
    print('Highest Score is :', iScore)
    writeFile.write(str(iScore))
    writeFile.close()
    pygame.quit()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66414244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档