1

I'm using

    os.system('powershell.exe "Invoke-WebRequest -Uri Link')

And I wounder if you can make link a variable I already defined. My whole code looks like this

import os
link=input("What link do you want? ") 
os.system('powershell.exe "Invoke-WebRequest -Uri link')

But I don't know how (if it's possible) to put a variable in there?

2
  • I have never tried it with an os.system command but to insert a var in a string you can do this: 'first part of string'+my_var+'rest of the string' Commented Jan 7, 2022 at 20:03
  • What about an f-string? os.system(f'powershell -Command Invoke-WebRequest -Uri "{link}" -UseBasicParsing') Commented Jan 7, 2022 at 20:04

2 Answers 2

1

You could simply define a variable that holds the whole string you want to run.

input_var = str(input("Write -> "))
basic_command = "'powershell.exe " + '"Invoke-WebRequest -Uri '
full_command = basic_command + input_var + '"' + "'"
os.system(full_command)

In input_var we convert input into a string. In basic command we set the first part of the command you want to run. In full_command we add the input_var and then we close the whole line with proper " '. And then we run os system with the full command line.

I hope this helps you. I use this concept in lots of scripts

Sign up to request clarification or add additional context in comments.

Comments

1

Solution using a formatted string in Python

import os
link = input("What link do you want? ") 
os.system('powershell.exe "Invoke-WebRequest -Uri {link}'.format(link = link))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.