{"id":648,"date":"2023-03-23T12:08:37","date_gmt":"2023-03-23T12:08:37","guid":{"rendered":"https:\/\/tinyytopic.com\/?p=648"},"modified":"2023-03-23T12:07:57","modified_gmt":"2023-03-23T12:07:57","slug":"how-do-i-execute-a-program-or-system-command-using-python","status":"publish","type":"post","link":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/","title":{"rendered":"How do I execute a program (or) system command using Python?"},"content":{"rendered":"\n<p style=\"font-size:15px\"><br>You can execute a program or system command using Python by using the <code>subprocess<\/code> module.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>Read Also, <a href=\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/what-are-the-ways-to-call-external-programs-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Several ways to call external programs in Python<\/a><\/p>\n\n\n\n<p style=\"font-size:15px\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><br>Here is an example of how to execute a system command:<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import subprocess\n\n# Run a program and pass arguments\nsubprocess.run([\"echo\", \"Hello, World!\"])\n\n# Run a system command with shell expansion\nsubprocess.run(\"echo $PATH\", shell=True)\n<\/pre>\n\n\n\n<p style=\"font-size:15px\">In this example, the <code>subprocess.run<\/code> function is used to execute the <code>echo<\/code> program with the argument <code>\"Hello, World!\"<\/code>. The <code>subprocess.run<\/code> function can also be used to run a system command with shell expansion. In this case, the <code>echo $PATH<\/code> command is executed, which prints the system&#8217;s PATH environment variable.<\/p>\n\n\n\n<p style=\"font-size:15px\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><br>Here is another example:<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import subprocess\n\n# Run a system command and store the output in a variable\noutput = subprocess.check_output(\"ls -la\", shell=True)\n\n# Print the output\nprint(output.decode('utf-8'))\n<\/pre>\n\n\n\n<p style=\"font-size:15px\">In this example, the <code>subprocess.check_output<\/code> function is used to execute the system command <code>ls -la<\/code>. The <code>shell=True<\/code> argument is used to run the command in a shell. The output of the command is stored in the <code>output<\/code> variable as a bytes object. The <code>decode('utf-8')<\/code> method is used to convert the bytes object to a string, which is then printed to the console.<\/p>\n\n\n\n<p style=\"font-size:15px\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><br>Here is another example to run a command and get its output:<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import subprocess\n\n# Run the command and capture its output\nresult = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)\n\n# Get the output as a string\noutput = result.stdout.decode('utf-8')\n\n# Print the output\nprint(output)\n<\/pre>\n\n\n\n<p style=\"font-size:15px\">In this example, we are running the <code>ls -l<\/code> command using the <code>subprocess.run()<\/code> method. We are using the <code>stdout<\/code> argument to capture the command&#8217;s output. We then use the <code>decode()<\/code> method to convert the byte string to a regular string, and then print the output to the console.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>You can replace the command with any other system command or program that you want to run. Just replace the <code>['ls', '-l']<\/code> argument with the command you want to run and its arguments. The <code>subprocess.run()<\/code> method will return a <code>CompletedProcess<\/code> object, which contains information about the command&#8217;s execution, including its output. You can access the output using the <code>stdout<\/code> attribute of the <code>CompletedProcess<\/code> object.<\/p>\n\n\n\n<p style=\"font-size:15px\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><br><strong>Here is another example to run a command and get its output and error:<\/strong><\/mark><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import subprocess\n\n# Run the command and capture its output and error messages\nresult = subprocess.run(['ls', '-l', '\/nonexistent'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n\n# Get the output and error messages as strings\noutput = result.stdout.decode('utf-8')\nerrors = result.stderr.decode('utf-8')\n\n# Print the output and error messages\nprint('Output:', output)\nprint('Errors:', errors)\n<\/pre>\n\n\n\n<p style=\"font-size:15px\">In this example, we are running the <code>ls -l \/nonexistent<\/code> command, which will generate an error message because the <code>\/nonexistent<\/code> directory does not exist. We are using the <code>stdout<\/code> and <code>stderr<\/code> arguments to capture both the output and any error messages that might be generated.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>We then use the <code>decode()<\/code> method to convert the byte strings to regular strings, and print the output and error messages to the console. If the command does not generate any errors, the <code>errors<\/code> variable will be an empty string.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>You can replace the command with any other system command or program that you want to run. Just replace the <code>['ls', '-l', '\/nonexistent']<\/code> argument with the command you want to run and its arguments. The <code>subprocess.run()<\/code> method will return a <code>CompletedProcess<\/code> object, which contains information about the command&#8217;s execution, including its output and any error messages. You can access the output and error messages using the <code>stdout<\/code> and <code>stderr<\/code> attributes of the <code>CompletedProcess<\/code> object.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>Read Also, <a href=\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/what-are-the-ways-to-call-external-programs-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Several ways to call external programs in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can execute a program or system command using Python by using the subprocess module. Read Also, Several ways to call external programs in Python Here is an example of how to execute a system command: In this example, the subprocess.run function is used to execute the echo program with the argument &#8220;Hello, World!&#8221;. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[42,12],"tags":[83,84,14,13,16,15],"class_list":["post-648","post","type-post","status-publish","format-standard","hentry","category-general-qa","category-python","tag-execute-command","tag-execute-program","tag-programming-language","tag-python","tag-python-code","tag-python-sample-code"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How do I execute a program (or) system command using Python? - tinyytopic.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How do I execute a program (or) system command using Python? - tinyytopic.com\" \/>\n<meta property=\"og:description\" content=\"You can execute a program or system command using Python by using the subprocess module. Read Also, Several ways to call external programs in Python Here is an example of how to execute a system command: In this example, the subprocess.run function is used to execute the echo program with the argument &quot;Hello, World!&quot;. The [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/\" \/>\n<meta property=\"og:site_name\" content=\"tinyytopic.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-23T12:08:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-23T12:07:57+00:00\" \/>\n<meta name=\"author\" content=\"tinyytopic.com\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tinyytopic.com\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/\",\"url\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/\",\"name\":\"How do I execute a program (or) system command using Python? - tinyytopic.com\",\"isPartOf\":{\"@id\":\"https:\/\/tinyytopic.com\/#website\"},\"datePublished\":\"2023-03-23T12:08:37+00:00\",\"dateModified\":\"2023-03-23T12:07:57+00:00\",\"author\":{\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\"},\"breadcrumb\":{\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tinyytopic.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How do I execute a program (or) system command using Python?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tinyytopic.com\/#website\",\"url\":\"https:\/\/tinyytopic.com\/\",\"name\":\"tinyytopic.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tinyytopic.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\",\"name\":\"tinyytopic.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"caption\":\"tinyytopic.com\"},\"sameAs\":[\"http:\/\/tinyytopic.com\"],\"url\":\"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How do I execute a program (or) system command using Python? - tinyytopic.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/","og_locale":"en_US","og_type":"article","og_title":"How do I execute a program (or) system command using Python? - tinyytopic.com","og_description":"You can execute a program or system command using Python by using the subprocess module. Read Also, Several ways to call external programs in Python Here is an example of how to execute a system command: In this example, the subprocess.run function is used to execute the echo program with the argument \"Hello, World!\". The [&hellip;]","og_url":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/","og_site_name":"tinyytopic.com","article_published_time":"2023-03-23T12:08:37+00:00","article_modified_time":"2023-03-23T12:07:57+00:00","author":"tinyytopic.com","twitter_card":"summary_large_image","twitter_misc":{"Written by":"tinyytopic.com","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/","url":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/","name":"How do I execute a program (or) system command using Python? - tinyytopic.com","isPartOf":{"@id":"https:\/\/tinyytopic.com\/#website"},"datePublished":"2023-03-23T12:08:37+00:00","dateModified":"2023-03-23T12:07:57+00:00","author":{"@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb"},"breadcrumb":{"@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/how-do-i-execute-a-program-or-system-command-using-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tinyytopic.com\/"},{"@type":"ListItem","position":2,"name":"How do I execute a program (or) system command using Python?"}]},{"@type":"WebSite","@id":"https:\/\/tinyytopic.com\/#website","url":"https:\/\/tinyytopic.com\/","name":"tinyytopic.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tinyytopic.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb","name":"tinyytopic.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","caption":"tinyytopic.com"},"sameAs":["http:\/\/tinyytopic.com"],"url":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"}]}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"tinyytopic.com","author_link":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"},"uagb_comment_info":20,"uagb_excerpt":"You can execute a program or system command using Python by using the subprocess module. Read Also, Several ways to call external programs in Python Here is an example of how to execute a system command: In this example, the subprocess.run function is used to execute the echo program with the argument \"Hello, World!\". The&hellip;","_links":{"self":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/comments?post=648"}],"version-history":[{"count":6,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/648\/revisions"}],"predecessor-version":[{"id":656,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/648\/revisions\/656"}],"wp:attachment":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/media?parent=648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/categories?post=648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/tags?post=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}