首页 > 解决方案 > 为什么每次尝试替换文件中的文本时程序都会崩溃?

问题描述

我正在使用 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但错误是一样的。

标签: pythonclientminecraft

解决方案


推荐阅读