[英]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.