How to send value from python script to console and exitCalling an external command in PythonHow can I safely create a nested directory?How to get the current time in PythonHow can I make a time delay in Python?How to clear the interpreter console?How do I sort a dictionary by value?How to substring a string in Python?How to leave/exit/deactivate a Python virtualenvHow do I concatenate two lists in Python?How do I load a file into the python console?Why is reading lines from stdin much slower in C++ than Python?
What was the Shuttle Carrier Aircraft escape tunnel?
Do I need a shock-proof watch for cycling?
"How can you guarantee that you won't change/quit job after just couple of months?" How to respond?
What could exist inside and between the walls of a Dyson Sphere?
Dates on degrees don’t make sense – will people care?
What happens to Cessna electric flaps that are moving when power is lost?
Ndsolve problem with Sign (friction)
Unusual mail headers, evidence of an attempted attack. Have I been pwned?
Is this proposal by U.S. presidential candidate Pete Buttigieg to change the composition of the Supreme Court constitutional?
How to model a twisted cylinder like this
What is "industrial ethernet"?
Helping ease my back pain when I'm studying 13 hours everyday, even weekends
Why do textbooks often include the solutions to odd or even numbered problems but not both?
Explain why a line can never intersect a plane in exactly two points.
Silly doubt about tidal effects and Einstein Field Equations
What does it mean to "control target player"?
Same EPSG code for different objects
What can I do with a research project that is my university’s intellectual property?
What exactly is the 'online' in OLAP and OLTP?
Does having had a visa for a country mean I used to be a citizen/national of that country?
Why do all the teams that I have worked with always finish a sprint without completion of all the stories?
If I wouldn't want to read the story, is writing it still a good idea?
Is "Busen" just the area between the breasts?
Count All Possible Unique Combinations of Letters in a Word
How to send value from python script to console and exit
Calling an external command in PythonHow can I safely create a nested directory?How to get the current time in PythonHow can I make a time delay in Python?How to clear the interpreter console?How do I sort a dictionary by value?How to substring a string in Python?How to leave/exit/deactivate a Python virtualenvHow do I concatenate two lists in Python?How do I load a file into the python console?Why is reading lines from stdin much slower in C++ than Python?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
My python script generate a proper command that user need to run in the same console. My scenario is that user is running a script and then as a result see the command that must to run. Is there any way to exit python script and send that command to console, so user do not need to copy/paste?
python
add a comment |
My python script generate a proper command that user need to run in the same console. My scenario is that user is running a script and then as a result see the command that must to run. Is there any way to exit python script and send that command to console, so user do not need to copy/paste?
python
1
Why not just usesubprocessto execute the command without the user having to press Enter?
– BoarGules
Mar 25 at 8:39
Python 2 or Python 3? What version are you using? What minor version, more specifically?
– 0 0
May 2 at 12:09
The answer may depend on the operation system and/or command line you are using. In Linux, you could just pipe the output of the script to a shell interpreter:python script.py | sh
– tobias_k
May 2 at 12:12
add a comment |
My python script generate a proper command that user need to run in the same console. My scenario is that user is running a script and then as a result see the command that must to run. Is there any way to exit python script and send that command to console, so user do not need to copy/paste?
python
My python script generate a proper command that user need to run in the same console. My scenario is that user is running a script and then as a result see the command that must to run. Is there any way to exit python script and send that command to console, so user do not need to copy/paste?
python
python
edited May 2 at 12:30
0 0
2,1571619
2,1571619
asked Mar 25 at 8:23
route00route00
52
52
1
Why not just usesubprocessto execute the command without the user having to press Enter?
– BoarGules
Mar 25 at 8:39
Python 2 or Python 3? What version are you using? What minor version, more specifically?
– 0 0
May 2 at 12:09
The answer may depend on the operation system and/or command line you are using. In Linux, you could just pipe the output of the script to a shell interpreter:python script.py | sh
– tobias_k
May 2 at 12:12
add a comment |
1
Why not just usesubprocessto execute the command without the user having to press Enter?
– BoarGules
Mar 25 at 8:39
Python 2 or Python 3? What version are you using? What minor version, more specifically?
– 0 0
May 2 at 12:09
The answer may depend on the operation system and/or command line you are using. In Linux, you could just pipe the output of the script to a shell interpreter:python script.py | sh
– tobias_k
May 2 at 12:12
1
1
Why not just use
subprocess to execute the command without the user having to press Enter?– BoarGules
Mar 25 at 8:39
Why not just use
subprocess to execute the command without the user having to press Enter?– BoarGules
Mar 25 at 8:39
Python 2 or Python 3? What version are you using? What minor version, more specifically?
– 0 0
May 2 at 12:09
Python 2 or Python 3? What version are you using? What minor version, more specifically?
– 0 0
May 2 at 12:09
The answer may depend on the operation system and/or command line you are using. In Linux, you could just pipe the output of the script to a shell interpreter:
python script.py | sh– tobias_k
May 2 at 12:12
The answer may depend on the operation system and/or command line you are using. In Linux, you could just pipe the output of the script to a shell interpreter:
python script.py | sh– tobias_k
May 2 at 12:12
add a comment |
2 Answers
2
active
oldest
votes
A solution would be to have your python script (let's call it script.py) just print the command: print('ls -l') and use it in a terminal like so: $(python3 script.py). This makes bash run the output of your script as a command, and would basically run a ls -l in the terminal.
You can even go a step beyond and create an alias in ~/.bashrc so that you no longer need to call the whole line. You can write at the end of the file something like alias printls=$(python3 /path/to/script.py). After starting a new terminal, you can type printls and the script will run.
A drawback of this method is that you have no proper way of handling exceptions or errors in your code, since everything it prints will be run as a command. One way (though ugly) would be to print('echo "An error occured!"') so that the user who runs the command can see that something malfunctioned.
However, I'd suggest going for the "traditional" way and running the command directly from python. Here's a link to how you can achieve this: Calling an external command in Python.
add a comment |
Python can run system commands in new subshells. The proper way of doing this is via the subprocess module, but for simple tasks it's easier to just use os.system. Example Python script (assuming a Unix-like system):
import os
os.system('ls')
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f55333703%2fhow-to-send-value-from-python-script-to-console-and-exit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
A solution would be to have your python script (let's call it script.py) just print the command: print('ls -l') and use it in a terminal like so: $(python3 script.py). This makes bash run the output of your script as a command, and would basically run a ls -l in the terminal.
You can even go a step beyond and create an alias in ~/.bashrc so that you no longer need to call the whole line. You can write at the end of the file something like alias printls=$(python3 /path/to/script.py). After starting a new terminal, you can type printls and the script will run.
A drawback of this method is that you have no proper way of handling exceptions or errors in your code, since everything it prints will be run as a command. One way (though ugly) would be to print('echo "An error occured!"') so that the user who runs the command can see that something malfunctioned.
However, I'd suggest going for the "traditional" way and running the command directly from python. Here's a link to how you can achieve this: Calling an external command in Python.
add a comment |
A solution would be to have your python script (let's call it script.py) just print the command: print('ls -l') and use it in a terminal like so: $(python3 script.py). This makes bash run the output of your script as a command, and would basically run a ls -l in the terminal.
You can even go a step beyond and create an alias in ~/.bashrc so that you no longer need to call the whole line. You can write at the end of the file something like alias printls=$(python3 /path/to/script.py). After starting a new terminal, you can type printls and the script will run.
A drawback of this method is that you have no proper way of handling exceptions or errors in your code, since everything it prints will be run as a command. One way (though ugly) would be to print('echo "An error occured!"') so that the user who runs the command can see that something malfunctioned.
However, I'd suggest going for the "traditional" way and running the command directly from python. Here's a link to how you can achieve this: Calling an external command in Python.
add a comment |
A solution would be to have your python script (let's call it script.py) just print the command: print('ls -l') and use it in a terminal like so: $(python3 script.py). This makes bash run the output of your script as a command, and would basically run a ls -l in the terminal.
You can even go a step beyond and create an alias in ~/.bashrc so that you no longer need to call the whole line. You can write at the end of the file something like alias printls=$(python3 /path/to/script.py). After starting a new terminal, you can type printls and the script will run.
A drawback of this method is that you have no proper way of handling exceptions or errors in your code, since everything it prints will be run as a command. One way (though ugly) would be to print('echo "An error occured!"') so that the user who runs the command can see that something malfunctioned.
However, I'd suggest going for the "traditional" way and running the command directly from python. Here's a link to how you can achieve this: Calling an external command in Python.
A solution would be to have your python script (let's call it script.py) just print the command: print('ls -l') and use it in a terminal like so: $(python3 script.py). This makes bash run the output of your script as a command, and would basically run a ls -l in the terminal.
You can even go a step beyond and create an alias in ~/.bashrc so that you no longer need to call the whole line. You can write at the end of the file something like alias printls=$(python3 /path/to/script.py). After starting a new terminal, you can type printls and the script will run.
A drawback of this method is that you have no proper way of handling exceptions or errors in your code, since everything it prints will be run as a command. One way (though ugly) would be to print('echo "An error occured!"') so that the user who runs the command can see that something malfunctioned.
However, I'd suggest going for the "traditional" way and running the command directly from python. Here's a link to how you can achieve this: Calling an external command in Python.
answered Mar 25 at 11:20
BogsanBogsan
33127
33127
add a comment |
add a comment |
Python can run system commands in new subshells. The proper way of doing this is via the subprocess module, but for simple tasks it's easier to just use os.system. Example Python script (assuming a Unix-like system):
import os
os.system('ls')
add a comment |
Python can run system commands in new subshells. The proper way of doing this is via the subprocess module, but for simple tasks it's easier to just use os.system. Example Python script (assuming a Unix-like system):
import os
os.system('ls')
add a comment |
Python can run system commands in new subshells. The proper way of doing this is via the subprocess module, but for simple tasks it's easier to just use os.system. Example Python script (assuming a Unix-like system):
import os
os.system('ls')
Python can run system commands in new subshells. The proper way of doing this is via the subprocess module, but for simple tasks it's easier to just use os.system. Example Python script (assuming a Unix-like system):
import os
os.system('ls')
answered May 2 at 12:37
jmd_dkjmd_dk
4,09322545
4,09322545
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55333703%2fhow-to-send-value-from-python-script-to-console-and-exit%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
1
Why not just use
subprocessto execute the command without the user having to press Enter?– BoarGules
Mar 25 at 8:39
Python 2 or Python 3? What version are you using? What minor version, more specifically?
– 0 0
May 2 at 12:09
The answer may depend on the operation system and/or command line you are using. In Linux, you could just pipe the output of the script to a shell interpreter:
python script.py | sh– tobias_k
May 2 at 12:12