use variable cmd command with python

Asked

Viewed 764 times

1

I wanted to make a program that opens and reads a text file. Then run or command on cmd using what he had read in the text file more or less 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'
)  
  • What you tried to do and why it didn’t work?

  • i would write the name of wifi in the file name.txt . the program would read this file store in the seed1 after it comes from a command in cmd : netsh wlan export profile name="HERE IS THE NOME1 VARIABLE VALUE" ...

  • But what’s the difficulty? In playing the value of the variable within the command?

  • exactly. I wanted to play the variable in the command the

2 answers

2

Since the difficulty is only to insert the value of the variable nome1 at the command, the solution is to use the method format of type objects 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))  

1

You can do it like 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))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.