python - 为什么显示 io.UnsupportedOperation: not writable
问题描述
导入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.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()
推荐阅读
- c# - 使用 IAM 服务对用户应用程序进行身份验证
- javascript - Avada WP 主题/引导模式导致页面在移动设备上滚动到顶部
- c - 嵌套的 strtok_r():连续的标记包含父分隔符
- postgresql - PostgreSQL 驱动程序在每个连接上运行 SQL
- linux - while循环如何等待进程
- python - Python kmeans包安装失败
- token - 使用 web3 / rpc 跟踪以太坊区块链中代币的流通
- excel - 改进 SUMPRODUCT 语句以返回多个条件
- applescript - AppleScript 可以通过本地网络启动 Quicktime 播放吗?
- visual-c++ - 从版本控制接收时缺少 Visual C++ 附加目录