use variable in cmd command with python

0

I wanted to make a program that opens and reads a text file. Then run or command in cmd using what he had read in the text file something like this:

import os  
nome = open ('nome.txt')  
nome1 =nome.readline()  
nome.close()  
comando =os.system ('netsh wlan export profile name="AQUI FICA O VALOR DA VARIAVEL NOME1" folder="G:\WLess" key=clear'
)  
    
asked by anonymous 12.02.2017 / 23:02

2 answers

1

Since the only difficulty is to enter the value of the nome1 variable in the command, the solution is to use the format method of objects type string .

>>> nome1 = "nome_no_arquivo"
>>> "O valor da variável é {}".format(nome1)
O valor da variável é nome_no_arquivo

For your specific case:

comando =os.system ('netsh wlan export profile name="{}" folder="G:\WLess" key=clear'.format(nome1))  
    
13.02.2017 / 00:20
1

You can do this:

import os

with open('nome.txt', 'r') as f:
    nome = f.readlines()[0].strip()
    cmd = os.system('netsh wlan export profile name="{}" folder="G:\WLess" key=clear'.format(nome))
    
13.02.2017 / 00:29