I am trying to write multiple functions to a file based on user input. Here the problem is the code which write to a file, is not left aligned. maybe missing few basic elements.
Here is the code:
import requests
G=input("Define Count")
for k in range(0,G):
perf=G
if k==0 :
string = """
### RUN ####
def run():
d = collections.OrderedDict()
d['run']= 123,
return d
URL = "https://..../run"
headers = {"Content-Type":"application/json",
"Authorization": Token}
payload = json.dumps([run() for n in range(%s)])
resp = requests.post(URL, headers = headers ,data = payload))
if resp.status_code = 200:
print('Fail: ' + str(resp.status_code)+ str(resp.text))
else:
print('Pass' + str(resp.status_code)+ str(resp.text))
"""
string = Template % (Perf)
with open(path, 'a') as f:
f.write(string)
elif k==1:
string = """
### STOP ####
def stop():
d = collections.OrderedDict()
d['STOP']= 123,
d['wait']=20
return d
URL = "https://..../stop"
headers = {"Content-Type":"application/json",
"Authorization": Token}
payload = json.dumps([stop() for n in range(%s)])
resp = requests.post(URL, headers = headers ,data = payload))
if resp.status_code = 200:
print('Fail: ' + str(resp.status_code)+ str(resp.text))
else:
print('Pass' + str(resp.status_code)+ str(resp.text))
"""
string = Template % (Perf)
with open(path, 'a') as f:
f.write(string)
elfif k=.....
when execute in loop, I expect the output file after write, with left aligned as :
def run():
....
....
def stop():
....
....
def wait():
....
but when i excute in loop, i do get as :
def run():
....
....
def stop():
....
....
def run():
....
....
def stop():
....
....
Am not sure why i get with irregular indent. pls help me