Run a shell command in Python
Environment - PyCharm
I am using the below command to get the JSON file:-
aws rds describe-db-cluster-snapshots > snapshotdetails.json
I am using this Json file to extract some data. I want the above command to run from my Python script. I have tried below but its failing:-
from subprocess import call
call(["aws rds describe-db-cluster-snapshots > snapshotdetails.json"])
I am getting error and its not working. Any pointers??
Error:-
Traceback (most recent call last):File "/Users/PrashastKumar/Desktop/CrossRegion/venv/lib/crossregiontrial.py", line 3, in <module>
call(["aws rds describe-db-cluster-snapshots > snapshotdetails.txt"])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
shell python aws
|
show 4 more comments
Environment - PyCharm
I am using the below command to get the JSON file:-
aws rds describe-db-cluster-snapshots > snapshotdetails.json
I am using this Json file to extract some data. I want the above command to run from my Python script. I have tried below but its failing:-
from subprocess import call
call(["aws rds describe-db-cluster-snapshots > snapshotdetails.json"])
I am getting error and its not working. Any pointers??
Error:-
Traceback (most recent call last):File "/Users/PrashastKumar/Desktop/CrossRegion/venv/lib/crossregiontrial.py", line 3, in <module>
call(["aws rds describe-db-cluster-snapshots > snapshotdetails.txt"])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
shell python aws
Not without seeing the error.
– DopeGhoti
May 8 '18 at 21:57
@JohnMiliter: you mean to say i need to use below
– Prashast
May 8 '18 at 22:01
call(["aws", "rds", "describe-db-cluster-snapshots" > snapshotdetails.txt"])
– Prashast
May 8 '18 at 22:02
call(["aws", "rds", "describe-db-cluster-snapshots", ">snapshotdetails.txt"]) -- used this , but getting error "Unknown options: >snapshotdetails.txt"
– Prashast
May 8 '18 at 22:03
1
stackoverflow.com/questions/89228/…
– John Militer
May 8 '18 at 22:07
|
show 4 more comments
Environment - PyCharm
I am using the below command to get the JSON file:-
aws rds describe-db-cluster-snapshots > snapshotdetails.json
I am using this Json file to extract some data. I want the above command to run from my Python script. I have tried below but its failing:-
from subprocess import call
call(["aws rds describe-db-cluster-snapshots > snapshotdetails.json"])
I am getting error and its not working. Any pointers??
Error:-
Traceback (most recent call last):File "/Users/PrashastKumar/Desktop/CrossRegion/venv/lib/crossregiontrial.py", line 3, in <module>
call(["aws rds describe-db-cluster-snapshots > snapshotdetails.txt"])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
shell python aws
Environment - PyCharm
I am using the below command to get the JSON file:-
aws rds describe-db-cluster-snapshots > snapshotdetails.json
I am using this Json file to extract some data. I want the above command to run from my Python script. I have tried below but its failing:-
from subprocess import call
call(["aws rds describe-db-cluster-snapshots > snapshotdetails.json"])
I am getting error and its not working. Any pointers??
Error:-
Traceback (most recent call last):File "/Users/PrashastKumar/Desktop/CrossRegion/venv/lib/crossregiontrial.py", line 3, in <module>
call(["aws rds describe-db-cluster-snapshots > snapshotdetails.txt"])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
shell python aws
shell python aws
edited May 8 '18 at 21:59
Prashast
asked May 8 '18 at 21:53
PrashastPrashast
246
246
Not without seeing the error.
– DopeGhoti
May 8 '18 at 21:57
@JohnMiliter: you mean to say i need to use below
– Prashast
May 8 '18 at 22:01
call(["aws", "rds", "describe-db-cluster-snapshots" > snapshotdetails.txt"])
– Prashast
May 8 '18 at 22:02
call(["aws", "rds", "describe-db-cluster-snapshots", ">snapshotdetails.txt"]) -- used this , but getting error "Unknown options: >snapshotdetails.txt"
– Prashast
May 8 '18 at 22:03
1
stackoverflow.com/questions/89228/…
– John Militer
May 8 '18 at 22:07
|
show 4 more comments
Not without seeing the error.
– DopeGhoti
May 8 '18 at 21:57
@JohnMiliter: you mean to say i need to use below
– Prashast
May 8 '18 at 22:01
call(["aws", "rds", "describe-db-cluster-snapshots" > snapshotdetails.txt"])
– Prashast
May 8 '18 at 22:02
call(["aws", "rds", "describe-db-cluster-snapshots", ">snapshotdetails.txt"]) -- used this , but getting error "Unknown options: >snapshotdetails.txt"
– Prashast
May 8 '18 at 22:03
1
stackoverflow.com/questions/89228/…
– John Militer
May 8 '18 at 22:07
Not without seeing the error.
– DopeGhoti
May 8 '18 at 21:57
Not without seeing the error.
– DopeGhoti
May 8 '18 at 21:57
@JohnMiliter: you mean to say i need to use below
– Prashast
May 8 '18 at 22:01
@JohnMiliter: you mean to say i need to use below
– Prashast
May 8 '18 at 22:01
call(["aws", "rds", "describe-db-cluster-snapshots" > snapshotdetails.txt"])
– Prashast
May 8 '18 at 22:02
call(["aws", "rds", "describe-db-cluster-snapshots" > snapshotdetails.txt"])
– Prashast
May 8 '18 at 22:02
call(["aws", "rds", "describe-db-cluster-snapshots", ">snapshotdetails.txt"]) -- used this , but getting error "Unknown options: >snapshotdetails.txt"
– Prashast
May 8 '18 at 22:03
call(["aws", "rds", "describe-db-cluster-snapshots", ">snapshotdetails.txt"]) -- used this , but getting error "Unknown options: >snapshotdetails.txt"
– Prashast
May 8 '18 at 22:03
1
1
stackoverflow.com/questions/89228/…
– John Militer
May 8 '18 at 22:07
stackoverflow.com/questions/89228/…
– John Militer
May 8 '18 at 22:07
|
show 4 more comments
1 Answer
1
active
oldest
votes
A python3
solution:
from subprocess import Popen, PIPE
cmd_output = Popen(["echo", "foo"], stdout=PIPE)
with open('bar.txt', 'w') as out_handle:
out_handle.write(cmd_output.communicate()[0].decode('UTF-8'))
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442641%2frun-a-shell-command-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A python3
solution:
from subprocess import Popen, PIPE
cmd_output = Popen(["echo", "foo"], stdout=PIPE)
with open('bar.txt', 'w') as out_handle:
out_handle.write(cmd_output.communicate()[0].decode('UTF-8'))
add a comment |
A python3
solution:
from subprocess import Popen, PIPE
cmd_output = Popen(["echo", "foo"], stdout=PIPE)
with open('bar.txt', 'w') as out_handle:
out_handle.write(cmd_output.communicate()[0].decode('UTF-8'))
add a comment |
A python3
solution:
from subprocess import Popen, PIPE
cmd_output = Popen(["echo", "foo"], stdout=PIPE)
with open('bar.txt', 'w') as out_handle:
out_handle.write(cmd_output.communicate()[0].decode('UTF-8'))
A python3
solution:
from subprocess import Popen, PIPE
cmd_output = Popen(["echo", "foo"], stdout=PIPE)
with open('bar.txt', 'w') as out_handle:
out_handle.write(cmd_output.communicate()[0].decode('UTF-8'))
answered May 9 '18 at 4:15
user3479780user3479780
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442641%2frun-a-shell-command-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not without seeing the error.
– DopeGhoti
May 8 '18 at 21:57
@JohnMiliter: you mean to say i need to use below
– Prashast
May 8 '18 at 22:01
call(["aws", "rds", "describe-db-cluster-snapshots" > snapshotdetails.txt"])
– Prashast
May 8 '18 at 22:02
call(["aws", "rds", "describe-db-cluster-snapshots", ">snapshotdetails.txt"]) -- used this , but getting error "Unknown options: >snapshotdetails.txt"
– Prashast
May 8 '18 at 22:03
1
stackoverflow.com/questions/89228/…
– John Militer
May 8 '18 at 22:07