Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

298
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda