Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda