T.D. Nathan
T.D. Nathan

Reputation: 59

How to write a script from another script?

I need to execute below function based on user input:

If X=0, then from line URL ....Print('Success should be written to a file & get saved as test.py.

At the backend, the saved file (Test.py) would automatically get fetched by Task scheduler from the saved location & would run periodically.

And yes, we have many example to write a file / run python from another file, but couldn't get any resemblance to write the python script from another file.

I am sure missing few basic steps.

if x=0:
   ### Need to write below content to a file & save as test.py######
   URL = "https://.../login"
   headers  = {"Content-Type":"application/json"}
   params = {
   "userName":"xx",
   "password":"yy"
   }
   resp = requests.post(URL, headers = headers, data=json.dumps(params))
   if resp.status_code != 200:
       print('fail')
   else:
       print('Success')]
else:
   ### Need to write below content to a file ######
   URL = "https://.../login"
   headers  = {"Content-Type":"application/json"}
   params = {
   "userName":"RR",
   "password":"TT"
   }
   resp = requests.post(URL, headers = headers, data=json.dumps(params))
   if resp.status_code != 200:
       print('fail')
   else:
       print('Success')

Upvotes: 0

Views: 69

Answers (3)

Wups
Wups

Reputation: 2569

If you want to save it to a file, in the end, it must be a string.
Your two variations of the file look quite similar, so don't write it twice:

template ='''
URL = "https://.../login"
headers  = {"Content-Type":"application/json"}
params = {
   "userName":"%s",
   "password":"%s"
}
resp = requests.post(URL, headers = headers, data=json.dumps(params))
if resp.status_code != 200:
   print('fail')
else:
   print('Success')
'''

if x == 0:
    content = template % ("xx", "yy")
else:
    content = tempalte % ("RR", "TT")

with open("test.py", "w") as f:
   f.write(content)

Upvotes: 1

user5386938
user5386938

Reputation:

You can use triple-quotes to simplify things.

if x==0:
    path = "test.py"
    string = """\
import requests, json
URL = "https://.../login"
headers  = {"Content-Type":"application/json"}
params = {
    "userName":"xx",
    "password":"yy"
    }
resp = requests.post(URL, headers = headers, data=json.dumps(params))
if resp.status_code != 200:
    print('fail')
else:
    print('Success')
"""
else:
    path = "other.py"
    string = """\
import requests, json
URL = "https://.../login"
headers  = {"Content-Type":"application/json"}
params = {
    "userName":"RR",
    "password":"TT"
    }
resp = requests.post(URL, headers = headers, data=json.dumps(params))
if resp.status_code != 200:
    print('fail')
else:
    print('Success')
"""

with open(path, 'w') as f:
    f.write(string)


See docs. About a third of the way down the page.

Upvotes: 1

Fabricio Fonseca
Fabricio Fonseca

Reputation: 139

new_file = "print('line1')\n" \
           "print('line2')\n" \
           "print('line3')"

f = open('new_python.py', 'w')
print(new_file, file=f)

Upvotes: 1

Related Questions