python脚本在通过另一个python脚本启动后不会在文本文件中写入任何单词

2024-09-26 18:12:55 发布

您现在位置:Python中文网/ 问答频道 /正文

亲爱的社区:, 我需要一些帮助,在一个小人工智能测试计划,我想作为一个学校的项目

问题是,如果我通过另一个python文件启动一个python文件,那么应该启动的文件不会在.txt文件中写入任何内容。但是,如果我启动一个没有通过控制台写入任何内容的文件,它会令人惊讶地写入其条目并执行它应该执行的操作。有人能告诉我为什么一个程序不写任何东西,当我启动它与其他程序?我已经禁用了我的防病毒软件(AVG),但这没有帮助

这是我的密码:

一个程序正在启动另一个程序,该程序不写任何东西:

    import os
    import random
    import time
    for i in range(1):
        file = open('INPUT.txt','w')
        x=random.randint(1,3)
        if x==1:
            file.write("gelb")
            GesuchtesWort="Banane"
        elif x==2:
            file.write("rot")
            GesuchtesWort="Erdbeere"
        else:
            file.write("orange")
            GesuchtesWort="Orange"
        file.close()

        os.system('E:\7B\Informatik\Schlifelner\raspberry\AI_Test.py')
        time.sleep(1)

        file = open('RESULT.txt','r')
        if GesuchtesWort!=str(file.read()):
            file.close()
            file = open('MARK.txt','w')
            file.write("0")
        else:
            file.close()
            file = open('MARK.txt','w')
            file.write("1")
        file.close()

The one program that just writes through console:

import random
class InputLayer:
    def InputN1(string):
        Output=0
        x=0

        LetterList=[]
        for i in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
            LetterList.append(i)

        for i in string:
            x=x+1
            Output+=LetterList.index(i)*x
        return Output

class HiddenLayer:
    def HiddenN1(number,boolean):
        file = open('ChangingValue1.txt','r+')
        ChangingValue=float(file.read())
        Output=1/(1+number)
        if boolean and (Output>=ChangingValue):
            return Output
        elif boolean==False:
            x=(random.randint(-1,1)/1000)
            while x==0:
                x=(random.randint(-1,1)/1000)
            ChangingValue+=x
            #Eintrag
            file.seek(0)
            file.write(str(ChangingValue))
            file.close()


    def HiddenN2(number,boolean):
        file = open('ChangingValue2.txt','r+')
        ChangingValue=float(file.read())
        Output=1/(1+number)
        if boolean and (Output>=ChangingValue):
            return Output
        elif boolean==False:
            x=(random.randint(-1,1)/1000)
            while x==0:
                x=(random.randint(-1,1)/1000)  
            ChangingValue+=x
            #Eintrag
            file.seek(0)
            file.write(str(ChangingValue))
            file.close()

    def HiddenN3(number,boolean):
        file = open('ChangingValue3.txt','r+')
        ChangingValue=float(file.read())
        Output=1/(1+number)
        if boolean and (Output>=ChangingValue):
            return Output
        elif boolean==False:
            x=(random.randint(-1,1)/1000)
            while x==0:
                x=(random.randint(-1,1)/1000)
            ChangingValue+=x
            #Eintrag
            file.seek(0)
            file.write(str(ChangingValue))
            file.close()

class OutputLayer:
    def OutputN1(number):
        if number>0.5:
            return "Banane"
        elif number>0 and number<0.5:
            return "Erdbeere"
        else:
            return "Orange"

#print(InputLayer.InputN1("lefpA"))
#file = open('ChangingValue1.txt','r+')
#x=file.read()
#print(x)
#file.seek(0)
#file.write(str(5))
#file.close()

#Main

#gelb|rot|orange
file=open('INPUT.txt','r')
UserInput=str(file.read())
file.close()

Layer1Output = InputLayer.InputN1(UserInput)
num1=HiddenLayer.HiddenN1(Layer1Output,True)
num2=HiddenLayer.HiddenN2(Layer1Output,True)
num3=HiddenLayer.HiddenN3(Layer1Output,True)
print(str(num1)+","+str(num2)+","+str(num3))
file = open('RESULT.txt','w')
file.write(OutputLayer.OutputN1(num1+num2+num3))
print(OutputLayer.OutputN1(num1+num2+num3))
file.close()

file = open('MARK.txt','r')
if str(file.read())=="0":
    HiddenLayer.HiddenN1(Layer1Output,False)
    HiddenLayer.HiddenN2(Layer1Output,False)
    HiddenLayer.HiddenN3(Layer1Output,False)
file.close()

Tags: txtnumberclosereadoutputreturnifrandom
1条回答
网友
1楼 · 发布于 2024-09-26 18:12:55

您的问题在于这一行:

os.system('E:\7B\Informatik\Schlifelner\raspberry\AI_Test.py')

作为一个实验,看看当您使用print而不是os.system时会发生什么:

>>> print('E:\7B\Informatik\Schlifelner\raspberry\AI_Test.py')
aspberry\AI_Test.pyifelner

屏幕上不会显示“E:\7B\Informatik\schliflner\raspberry\AI_Test.py”。这是因为“\”在python字符串中是一个特殊的字符,所以这些“\”字符后面的所有字符(特别是“\r”,它是一个回车符)都不是您所想的那样

您需要转义所有“\”字符:

>>> print('E:\\7B\\Informatik\\Schlifelner\\raspberry\\AI_Test.py')
E:\7B\Informatik\Schlifelner\raspberry\AI_Test.py

或者,更好的是,在整个字符串前面加上“r”前缀,以使用“原始”字符串:

>>> print(r'E:\7B\Informatik\Schlifelner\raspberry\AI_Test.py')
E:\7B\Informatik\Schlifelner\raspberry\AI_Test.py

因此,您只需将这一行更改为:

os.system(r'E:\7B\Informatik\Schlifelner\raspberry\AI_Test.py')

(注意字符串前的“r”)

相关问题 更多 >

    热门问题