Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

301
Views
how to assign variable value in python in terminal commands?

My code:

import os
from xml.dom import minidom

doc = minidom.parse("jobs.xml")

job = doc.getElementsByTagName("job")[0]
id = job.getElementsByTagName("id")[0]
name = job.getElementsByTagName("name")[0]

id_data = id.firstChild.data
name_data = name.firstChild.data

os.system('curl --compressed -H "Accept: application/xml" -X GET "http://localhost:19888/ws/v1/history/mapreduce/jobs/"')
>>> /home/ankit/rrd-xml/task.xml

I want to get the commands like

os.system('curl --compressed -H "Accept: application/xml" -X GET "http://localhost:19888/ws/v1/history/mapreduce/jobs/< variable value of name_data >"') 
>>> /home/ankit/rrd-xml/task.xml

How can i do it in python itself?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Use single quotes around the command, or even triple quotes, so that the quotes that form part of the command are not confused with the Python string:

os.system('curl --compressed -H "Accept: application/xml" -X GET "http://localhost:19888/ws/v1/history/mapreduce/jobs/{}" >> /home/ankit/rrd-xml/task.xml'.format(name_data))

That will work, but it's not the best. It would be better for you to use Python to make the HTTP request. requests is a good module for this:

import requests

url = 'http://localhost:19888/ws/v1/history/mapreduce/jobs/{}'.format(name_data)
headers = {'Accept': 'application/xml'}

response = requests.get(url, headers=headers)

with open('/home/ankit/rrd-xml/task.xml', 'a') as outfile:
    outfile.write(response.content)

This code will GET the URL, setting the Accept header to application/xml and append the response to the file. Compression is requested by default.

over 4 years ago · Santiago Trujillo Report

0

What you want is a simple string formatting.

import os
from xml.dom import minidom

doc = minidom.parse("jobs.xml")

job = doc.getElementsByTagName("job")[0]
id = job.getElementsByTagName("id")[0]
name = job.getElementsByTagName("name")[0]

id_data = id.firstChild.data
name_data = name.firstChild.data

url = 'http://localhost:19888/ws/v1/history/mapreduce/jobs/{name}'.format(name=name_data)
cmd = 'curl --compressed -H "Accept: application/xml" -X GET "{url}"'.format(url=url)

os.system(cmd)
over 4 years ago · Santiago Trujillo Report

0

Use subprocess. The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using the os.system function.

import subprocess
import shlex

command = shlex.split('curl --compressed -H "Accept: application/xml" -X GET http://localhost:19888/ws/v1/history/mapreduce/jobs/')

with open('/home/ankit/rrd-xml/task.xml', 'a') as f:
    subprocess.Popen(command, stdout=f)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!