1

I have a variable in python, and I'm trying to open a subprocess and echo the variable, then create a file with the variable in it. I tried this:

subprocess.Popen(['echo "$var" > file.txt'], shell=True)

It creates the file, but it's empty. How can I get the result that I want?

1 Answers1

2

In Python you don't use $ sign to use a variable. Also when you want to embed variable into string, you cannot just simply use variable name in string. You should do something like that:

subprocess.Popen(['echo "{}" > file.txt'.format(var)], shell=True)

This is great website which will explain you how to use .format method.

mrc02_kr
  • 121
  • 9