Dear stackoverflow community, I need some help at a little AI Testing-programm I wanted to make as a school project.
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. 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.
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('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. 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.
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:
>>> 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.