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

通过另一个python脚本启动后,python脚本不会在文本文件中写入单词,python,python-3.x,Python,Python 3.x,亲爱的社区:, 我需要一些帮助,在一个小人工智能测试计划,我想作为一个学校的项目 问题是,如果我通过另一个python文件启动一个python文件,那么应该启动的文件不会在.txt文件中写入任何内容。但是,如果我启动一个没有通过控制台写入任何内容的文件,它会令人惊讶地写入其条目并执行它应该执行的操作。有人能告诉我为什么一个程序不写任何东西,当我启动它与其他程序?我已经禁用了我的AntivirusAVG,但这没有帮助 这是我的密码: 一个程序正在启动另一个程序,该程序不写任何东西: imp

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

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

这是我的密码:

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

    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时会发生什么:

您无法将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”

我建议您将代码简化为相关部分—一个运行另一个脚本的简单脚本,该脚本将字符串写入文件,然后查看它是否工作。如果它真的起作用,那么慢慢地把它建立到你现在的状态,看看它在哪里断裂。这会给你一个问题所在的线索。如果不起作用,则将其发布到堆栈溢出。你不应该在这里发布这样的完整程序。好吧,出于某种原因,当我使用命令os.system'AI_Test.py 1'而不是os.system'E:\7B\Informatik\schliflener\raspberry\AI_Test.py'时,它会起作用……啊。。。这就是你的线索!所以,它不喜欢完整的路径,我明白为什么。看看打印“E:\7B\Informatik\schliflenr\raspberry\AI_Test.py”时会发生什么。您无法将E:\7B\Informatik\schliflner\raspberry\AI_Test.py打印到屏幕上。这是因为\是python字符串中的一个特殊字符。您需要使用一个额外的\e,即使用'e:\\7B\\Informatik\\schliflner\\raspberry\\AI_Test.py'来转义它,或者,更好的是,在整个字符串前面加一个'r'来使用一个'raw'字符串,即使用r'e:\7B\Informatik\schliflner\raspberry\AI_Test.py'Ah好的,我不知道,非常感谢
os.system(r'E:\7B\Informatik\Schlifelner\raspberry\AI_Test.py')