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;








-1















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?










share|improve this question



















  • 1





    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











  • 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















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?










share|improve this question



















  • 1





    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











  • 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








-1


0






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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











  • 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





    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











  • 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












2 Answers
2






active

oldest

votes


















0














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.






share|improve this answer






























    0














    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')





    share|improve this answer

























      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
      );



      );













      draft saved

      draft discarded


















      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









      0














      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.






      share|improve this answer



























        0














        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.






        share|improve this answer

























          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 11:20









          BogsanBogsan

          33127




          33127























              0














              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')





              share|improve this answer



























                0














                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')





                share|improve this answer

























                  0












                  0








                  0







                  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')





                  share|improve this answer













                  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')






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 2 at 12:37









                  jmd_dkjmd_dk

                  4,09322545




                  4,09322545



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                      155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해