python - 为什么每次尝试替换文件中的文本时程序都会崩溃?
问题描述
我正在使用 Windows 10 和 Python 3.9.0。我正在尝试用 Python 制作一个合法的 Minecraft 客户端。这就像自动行走、自动冲刺之类的东西。我有一个名为 module_state 的文件,如下所示:
walk = false
sprint = false
sneak = false
jump = false
auto_place = false
auto_hit = false
spam = false
它的工作原理是这样的:你启动一个名为 fim_client.py 的文件。当您这样做时,它会加载所需的所有内容并打开一个自定义外壳,如下所示:
Fim Client >
它还启动一个名为的文件,该文件fim_client_reader.pyw
读取module_state.md
并执行每个具有语句“True”的模块。但是每次我调用替换文件中文本的函数时,它都会崩溃。为什么?
代码:fi_client.py:
import time
import os
f = open('module_state.md')
def replacedatafile(f, txttf, txttr):
filedata = f.read()
newdata = filedata.replace(txttf, txttr)
f.write(newdata)
def return_cmd(dinput, f):
if dinput == 'help':
print('List of commands:')
print('Walk')
print('Sprint')
print('Jump')
elif dinput == 'sprint':
print('sprint enabled')
replacedatafile(f, "sprint = false", "sprint = true")
os.system('pause')
NULL = 'pause'
dinput = 'dump'
intest = '.'
print('Fim Client alpha testing')
print('Using python build 3.9.0')
print('Installing librarys...')
time.sleep(3)
print('Running module.test...')
os.system('title Fim Client alpha testing')
time.sleep(2.4)
print('''
Welcome to Fim Client alpha testing!
Type "help" for a list of commands.
''')
os.system('python fim_client_reader_.pyw')
while True:
dinput = input('Fim Client >')
return_cmd(dinput, f)
fim_client_reader.pyw:
################# IMPORT #################################
import os
import subprocess
################ DECLARE VARIABLES ######################################
f = open('module_state.md')
line = 'dum'
walk = False
sprint = False
sneak = False
jump = False
########################################## MAIN #################################
for i in range(7):
line = f.readline(i)
if line == 'sprint = True':
sprint = True
编辑:
它返回一个错误:
Traceback (most recent call last):
File "C:\Users\gusta\coding\MINECRAFT______UYUIK\Client\fim_client.py", line 61, in <module>
return_cmd(dinput, f)
File "C:\Users\gusta\coding\MINECRAFT______UYUIK\Client\fim_client.py", line 20, in return_cmd
replacedatafile(f, "sprint = false", "sprint = true")
File "C:\Users\gusta\coding\MINECRAFT______UYUIK\Client\fim_client.py", line 11, in replacedatafile
f.write(newdata)
io.UnsupportedOperation: not writable
编辑2:
我试图替换module_state.md
为,module_state.txt
但错误是一样的。
解决方案
推荐阅读
- css - 单击时如何从此div中删除红色边框?
- webview - Paypal 自适应支付结账页面未在移动 webview 中打开
- amazon-web-services - 配置 Terraform S3 后端时出错
- stripe-payments - Plaid + Stripe 从我的账户转移到 Plaid 认证的银行账户
- css - 我的页面没有反映我所做的任何 CSS 样式更改
- c# - 根据谁登录(基于 AD auth)锁定我的应用程序的某些功能
- windows - 尝试从 GitHub 克隆时出现权限被拒绝(公钥)错误
- c++ - 有没有办法在使用 Booth 算法的二进制乘法期间捕获溢出/下溢?
- android - 如何使用 Amazon Voice Service 或 Alexa 对我的应用进行语音查询
- python - 是否有 Python 函数可以根据条件过滤并将二进制值分配给数据集中的列?