【问题标题】:Why io.UnsupportedOperation: not writable is shown为什么显示 io.UnsupportedOperation: not writable
【发布时间】:2021-05-30 12:17:32
【问题描述】:

导入pygame,随机

定义相当游戏():

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" 显示在那里。

【问题讨论】:

    标签: python file write


    【解决方案1】:

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

    要以可写模式打开文件:

    writeFile = open("BestScore.txt","w") #Opens BestScore.txt in writable mode
    [...]#Other code
    

    所以最终的代码应该是这样的:

    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()
    

    【讨论】:

    • 非常感谢。是的,我会成功解决这个问题并修复错误。
    • 如果此答案对您有所帮助,那么您必须通过单击勾选按钮接受答案
    猜你喜欢
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 2017-12-07
    • 2021-12-01
    • 1970-01-01
    • 2015-06-14
    • 2012-12-06
    相关资源
    最近更新 更多