Use subprocess in python to echo a variable
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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?
linux shell-script python echo
New contributor
add a comment |Â
up vote
0
down vote
favorite
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?
linux shell-script python echo
New contributor
1
Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
â Mark Plotnick
1 hour ago
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
linux shell-script python echo
New contributor
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?
linux shell-script python echo
linux shell-script python echo
New contributor
New contributor
edited 12 mins ago
mrc02_kr
1,037320
1,037320
New contributor
asked 2 hours ago
david
1
1
New contributor
New contributor
1
Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
â Mark Plotnick
1 hour ago
add a comment |Â
1
Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
â Mark Plotnick
1 hour ago
1
1
Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
â Mark Plotnick
1 hour ago
Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
â Mark Plotnick
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
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.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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.
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.
answered 1 hour ago
mrc02_kr
1,037320
1,037320
add a comment |Â
add a comment |Â
david is a new contributor. Be nice, and check out our Code of Conduct.
david is a new contributor. Be nice, and check out our Code of Conduct.
david is a new contributor. Be nice, and check out our Code of Conduct.
david is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476393%2fuse-subprocess-in-python-to-echo-a-variable%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
â Mark Plotnick
1 hour ago