繁体   English   中英

通过另一个python脚本启动后,python脚本未在文本文件中写单词

[英]python script doesn't write a word in a text file after launching through another python script

亲爱的stackoverflow社区,我需要作为学校项目制作的一个小型AI测试程序,需要一些帮助。

问题是,如果我通过另一个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()

您的问题是与此行:

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 \\ Schlifelner \\ 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”)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM