简体   繁体   English

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

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

Dear stackoverflow community, I need some help at a little AI Testing-programm I wanted to make as a school project. 亲爱的stackoverflow社区,我需要作为学校项目制作的一个小型AI测试程序,需要一些帮助。

The problem is that if I launch a python file through another python file, the file that should be launching isn't writing anything in a .txt file. 问题是,如果我通过另一个python文件启动python文件,则应启动的文件未在.txt文件中写入任何内容。 However if I launch that one file that isn't writing anything trough the console it suprisingly writes its entry and does what it should do. 但是,如果我启动的那个没有通过控制台写入任何内容的文件,它会意外地写入其条目并执行应做的事情。 Could someone pls tell me why the one program doesn't write anything, when I launch it with the other program? 当我与另一个程序一起启动时,有人可以告诉我为什么一个程序不编写任何东西吗? I've already disabled my Antivirus(AVG) but that didn't help. 我已经禁用了我的防病毒(AVG),但没有帮助。

Here's my code: 这是我的代码:

The one program that is launching the other program, which doesn't write anything: 一个程序正在启动另一个程序,该程序不编写任何内容:

    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()

Your problem is with this line: 您的问题是与此行:

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

As an experiment, look at what happens when you use print instead of os.system : 作为实验,看看使用print而不是os.system时会发生什么:

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

You don't get "E:\\7B\\Informatik\\Schlifelner\\raspberry\\AI_Test.py" printed to the screen. 您不会在屏幕上看到“ E:\\ 7B \\ Informatik \\ Schlifelner \\ raspberry \\ AI_Test.py”。 That's because "\\" is a special character in python strings, so all the characters following those "\\" characters (in particular, the "\\r", which is a carriage return) are not doing what you think. 这是因为“ \\”是python字符串中的特殊字符,因此,所有在这些“ \\”字符之后的字符(特别是“ \\ r”,这是一个回车符)都无法满足您的要求。

You need to either escape all of the "\\" characters: 您需要转义所有“ \\”字符:

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

Or, even better, prefix the whole string with an 'r' to use a 'raw' string: 或者,甚至更好的是,在整个字符串前加上“ r”以使用“原始”字符串:

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

So you need to just change that one line to: 因此,您只需要将该行更改为:

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

(note the 'r' before the string) (请注意字符串前面的“ r”)

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

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