{"id":1537,"date":"2022-12-27T09:52:11","date_gmt":"2022-12-27T09:52:11","guid":{"rendered":"https:\/\/www.workfall.com\/learning\/blog\/?p=1537"},"modified":"2025-09-23T10:13:47","modified_gmt":"2025-09-23T10:13:47","slug":"how-to-execute-linux-commands-in-python","status":"publish","type":"post","link":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/","title":{"rendered":"How to Execute Linux Commands in Python?"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/LykK0jNte_cXw0hmskzNenJ9ic2ywyRuUlOKf1mGYPVWDjt-rO9mYEir9OIgmxxALvMZGQm5n4b6zJhhikReVtvoAOaoA35HH-hcG1FOiJshKfyt4OpJdrCpZmsX5nzUmmEAjjIqS2aa_3JKtaN--pnhTPHL4myDrLrJHG-jmT_z2LGtySHh_LWqme91HVTn\" alt=\"How to Execute Linux Commands in Python?\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">As of this writing, Linux has a global desktop market share of 2.77% (<a href=\"https:\/\/gs.statcounter.com\/os-market-share\/desktop\/worldwide\">A Report by Statcounter<\/a>), but it powers over 90% of all cloud infrastructure and hosting services. It is critical to be familiar with common Linux commands for this reason alone.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">According to a <a href=\"https:\/\/survey.stackoverflow.co\/2022\/#section-most-popular-technologies-operating-system\">2022 StackOverflow survey<\/a>, Linux-based operating systems are more popular than macOS, demonstrating the appeal of using open-source software by professional developers, with an impressive 39.89% market share.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Linux is a popular operating system among software developers and system administrators. It is widely used because it is open source, free and customizable, extremely robust, and adaptable. This makes it an excellent choice for servers, virtual machines (VMs), and a variety of other applications.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">As a result, knowing how to work with Linux is essential for anyone working in the tech industry because it is used almost everywhere.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">On the other hand, Python is a well-known and up-to-date programming language. It is especially popular among beginners because it is simple to learn. We can use Python to interact with the operating system because it is so powerful. Python serves as an alternative to the bash command language for scripting in Linux. Because it is a dependency on many tools and software, it is preinstalled in the majority of distributions. If not, it is simple to install. In this blog, we will demonstrate how to execute Linux commands in Python. Let\u2019s get started! \ud83d\ude80<\/p>\n\n\n\n<h2>What Is a Linux Command?<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">A command in Linux is a programme or utility that runs from the command line. A command line is an interface that accepts lines of text and converts them into computer instructions.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">Any graphical user interface (GUI) is simply a graphical representation of command-line programmes. For example, when you close a window by clicking the &#8220;X,&#8221; a command is executed behind the scenes.<\/p>\n\n\n\n<h3>Recent Stats on Linux<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/phIV1m4I2b7ldxdZVn4PI91jdYpobpanuPkHiYmf-f2nfq3ZOTHru3DAoSF2YehVUhDwg0V1VUnp_fuYFDMo8TwgfK4AffczQkMr0IBgcfRjYyfkYbcsiA42fwDBHCe2FfOQnDiAhM0FMXrsFAQUKtjhNYdswA46lPLG7mkhDWJf50k1Cvbc5oHcE6zPag87\" alt=\"Recent Stats on Linux\"\/><\/figure>\n\n\n\n<h3>Few Most Commonly Used Linux Commands<\/h3>\n\n\n\n<ul><li><em>ls<\/em> Command<\/li><li><em>alias<\/em> Command<\/li><li><em>unalias<\/em> Command<\/li><li><em>pwd<\/em> Command<\/li><li><em>cd<\/em> Command<\/li><li><em>cp<\/em> Command<\/li><li><em>rm<\/em> Command<\/li><li><em>mv<\/em> Command<\/li><li><em>mkdir<\/em> Command<\/li><li><em>man<\/em> Command<\/li><li><em>touch<\/em> Command<\/li><li><em>chmod<\/em> Command<\/li><li><em>.\/<\/em> Command<\/li><li><em>exit<\/em> Command<\/li><li><em>sudo<\/em> Command<\/li><li><em>shutdown<\/em> Command<\/li><li><em>htop<\/em> Command<\/li><li><em>unzip<\/em> Command<\/li><li><em>apt<\/em> , <em>yum<\/em> , <em>pacman<\/em> Commands<\/li><li><em>echo<\/em> Command<\/li><li><em>cat<\/em> Command<\/li><li><em>ps<\/em> Command<\/li><li><em>kill<\/em> Command<\/li><li><em>ping<\/em> Command<\/li><li><em>vim<\/em> Command<\/li><\/ul>\n\n\n\n<h2>Hands-on<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">In this hands-on, we will see how we can execute the ping Linux command in Python. We will begin with the creation of an empty directory. Creating a text file, we will store it in several valid server domains within the file. Then we will install a package if it doesn&#8217;t exist. Moving further, we will add the code creating a new file for the code. We will then alter the code based on whether or not we have administrator rights. Post that we will run the server and test out the execution for a single valid server followed by testing out the same for an invalid server. Altering the code, we will then execute the codebase onto a list of valid servers. On success, we will change the server list file to fill in valid and invalid servers. Post testing the same, finally we will pass in only invalid servers to test out the code.<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong><em>Note:<\/em><\/strong><em> Please ensure that Python is properly installed onto the local machine or else it might display multiple errors while following the process.<\/em><\/p>\n\n\n\n<p class=\"has-text-align-justify\">Create a new directory on the local machine.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/hyAUqeAMofsx5tXI4ohrVazrm9gSqgEz_NuThed4FNZ8BzA_YpFLcRDbeNLFHc1LKpTiEsB-SbmVRIhL1dkHer55zkb6DHkGOUh261AWIM6txKIKD4CJRjqxqrVVcv4mtOIK5qFwHC05Xi3yeqD1ZXGS9NdUQffJHEOaQ41jl1RsuIgirZ10W7B8w4LjFReL4v4kIl_K1A\" alt=\"\"\/><\/figure>\n\n\n\n<p>Open the directory in the code editor.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/Xn0q3OwVGZYRPb5U89qI6ahdxtff047N9rOjpBDhh89TjescLNyDAyv_Z7YAP7RFBcT_o4uLCL3fwGsaIm59hqoLSuA0aMHV-tGZmaYXx99m8CBM0E0AO-IVvdtLArHYfKr_pO_wOrD0O2mvNejPyIPP9GdYM5oElGZcvqcY42uTZKKzmVUp36CVO8cgPeqJmMRZq-eubA\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Right-click in the folder structure and click on the new file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/RwarugkpErDLVa007CRIYL_Z2U7XPe_6aVMHN1emjV00fbQFELsS7F4A2sR_ETpYSb6HBO2726XgfruYka-x2IEQ6XfPbp2ERHwRXLtQfc2s8fFw2sZMA2FOkDRgNE6I7iiRuFT3Y8wRewghBa7IupmK-Zdqr-dUj2b1DPuVeM0QNGPSWerNrmyqKfAdy0n9yr4KDkW0yw\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p>Enter a name for the file as shown below. We need to create a text file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/HSakYxQI4VBApDXNmt4mc0ZPqz9lBRs6KG0LAnExKvypFzKrzzhrhsZ9v_n36cYRUyLy5CfsF6Y1K7lCVnHOK_v4c48W6lU-kjdA2deTW-JK1OV0zMizsVSq2BUeMI-CLd4_U_3YD18tSAQW0VMVOfF9dMh8GHeNhmnGLSfBsKQ8nquARFGa_nP-hLKEQ2bcM30y_rZOqw\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p>In the newly created text file, add some domains within it as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/CfOipszk3LjydPIywTouAWgeOGHVvzo4Q7K1zV0bmFedlhj6Tp8fgZI_DG9sqC5yrVS-wO7ctSPugfe94JZ4grfs6xyF-Roz4y4erQaaIwlWc1MKM4-LSPwaGb2GTkCH44slOFoBZXyfGzIVt9axT7dirf9APx17X4PjXQRPiCnR7bz9EsuW-GV_mKog01V9EzduuBW5kw\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p>Right-click in the folder structure and again click on New File.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/GtQjoOTX6Gz70zQlR3z-kqFHczM6ao5Nj6CaD0ESrZsTZwuZNYOdELeeIFXl7gwA1zK3rh3CfdT6NEQ8PNzX1FpT-tbkL3Uu1iWCQZOcyixcsyUQ2n7GLWtST2_DcSNMojb3uzBaj7Y3lyCCxL9mHU52pKAP5qSPDKZ8IFtRy8-L3mgf9oOMlsvllmr7Tay-H8eRnHQ-dQ\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p>Create a <em>.py<\/em> file where we will be having our code ping servers via python using the Linux command.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/afu1xe6jdGsHLuAoou4XJ6Hksq0OrKK7ceyexgK9lynvpV18Yufu7UzhWfhYkulkdSj2DdzsNruZyoL4NZwajWBV3O3U3Gr_sdS861tiwmmaUqruDczBqwpW1wryWGUeQp-6GArC4UzFFky_cePXf1v4RNeTCj7xa99I9RBLjzO_Xi_WX30VvpCEmxJwEQY38px-DbBkTA\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">The subprocess package is usually inbuilt but in case you face any issues, run the below command to install it in the directory:<\/p>\n\n\n\n<p><code>pip install subprocess.run<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/PUtZCimaK32bhaq59PYvW4Oeh7wgN4Dh8XUbqfJqCUWhi4u9XEI4aYmCSoEx0LwX0YtARZ0eJXTOZH2lzoC1emqHzlqZCL6rIl9R5LRCxmwYHnC-C5wERBAgI0iHpZeAXPraEUjywl6VhNQUVJd0zwj5iqcdmJ95rmw4naiikREbq52gjKa10M9gXSuChM6O57cyC9Gdsg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, in the newly created Python file, import the subprocess package.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/_64RvoRvDdhYSIAte8OPNPPuVZXbXLJQ4tNHHHhBTpdJo6K2V6Se-Qwsc0H-fX6EN-NlYWyWhtwuvUuUQ_zp9p1iuDR3AgFEq-tg0NVazBFIE_vINmeyLwx_wPpPOtRsuisbUVymGJ00e2oClEbgY3m93HM8AiG02zCeMO8CjeLB9OGxrNMfTLyettNouihkf-zhWcc0rQ\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Define a new function as shown in the image below. This function will contain the code to ping the servers and return the response.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/nOcF3ukjeveMtoMLspfSD_Firlv8ig2Hz-8eQihuwAKTqcQdSX1Atg-gHfh8SPh4MNKHtANzwZI94kaakovBpOA_fI1a_MitAZDWeCdXBJ7iodSAFSGa7111MHjSIEbBk9NOuJJjJEJ00JJNhMTSLKM_3WiqO3mGKKuzDDqjKaf1ma0x6qE38Sxu9zOVNTAz-68SgkzXXQ\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p>Add a new variable with the command that we wish to execute.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/xPwOAlYyM_OvEGH_8baF-lP1fdCJxNHMgdbB6ij2mbxCeu0VV98mmdwgC9EXd7ZA4v9rMkb8Y2Rs2WmKlhxuqoGZ2VEwRhb_bOpjStLhOlKS_LI7AjEGJa_bws9Dvofsw8iksWZl75P6z-vXPz5Vxhpqc6qaD6AHMovP9hQrjEe7n-xcurOqKs3FPc5P1h-zNqIApXl4Cg\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p>If you have administrator access, add the following code so that the ping command sends only one package.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/Mwb5-N2sB5R4lETwguTnTz0DGubNEjyarx9w2NZSmznrwyoPee-K3rVadBPr9r_Nny3VtynEKBcEYdgrk2AArcg8d-Ij-eR_fOZBYZjPWUafYsE3FkVjUeSX_vtdW5mjSZV1Wf_WNkFAINKYTG1EViQuO4WXZt6p2Tr0uBAS0LyQgRwAj4kPcKzEBdBnQDeMdE5K809dNA\" alt=\"\"\/><\/figure>\n\n\n\n<p>We will then convert the output into the string format.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/pJ0-XqXRUXgjhfdRHyWLa7__uwYNLMedYF6UDUrf6FDcXgEt9vmXx_GIW5srSBXsEXfZ1K7oPPVm97hhmHuo9_cfxq2vw3ByrOIgzBXVDUgws42e6aEC45LqKNfYy1dAeyMb8uLolbfbOyQCi1hpqliz9WESIPzErQ-GBvS-JMs3-wr9iwRMYJlgQJ_mALIEAV56ygSGig\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p>Finally, we&#8217;ll append the result to the list of results and return the list.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/uP1RMz6pie61pyZ9HBYTHE2EeXuY2bQIFpYnCBFoHuUgUuTU1wpJyThWjnjbMDUoFlylEzPEeEdfA3XkFmDeHewSDV7uCbP2Sx5vV5fd76qvno6Ltz544bgqGqSbJFtZmGlI4_LLP8Z8eSjXmzfEQ6sedKpUGy0VlWuzhqqul7oHSuvlxIpkUTVDKOAq-D4e7DC5-d2dcw\" alt=\"\"\/><\/figure>\n\n\n\n<p>Initializing the main function, we will call the function that we created above passing a domain as the parameter.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/q8oTOkCwMKam25YyWMS1vCCl430hfKwJMpnSJsc6KliW7mAhBSCXJ67xCZQII1LpaDAmzeC0K0pSXW1Ko_SNVs-xsy0-JNymad6vveZvnEWF_8mohv58n29sTiIzM_7Y_VEt89zQcbd2x7SxSFAAkjqZg80FBuFgkSpGmwDGZz6ebk9h6pgx7qzjCfrpBLHYIqoj4XbRXA\" alt=\"\"\/><\/figure>\n\n\n\n<p>The final code looks like as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/mRRMhEWF-nW9raIyZk0WP1H6lv2nRg-Gltmr2m1kdCOaO13zhZ2kNG3X1ghMCg4IadBtbypdT4kXX8_Gm4nW5OVNNy7P5b0rlUKDDsTNr1T3AkRkbOmdbHkb2ddAViu6pEru5SxIgjFtFqU48d0YTzbuGhj6JRU91GzU1xxUtg6swzqZ3TkQDB-8txOjBzvOwOFLhG-5xw\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p>On success, we will run the command as shown below to execute the code.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/a0HNbkOsSkejLNW3-lSg0n3mZrCO4HCTt48R1EZ7YvBsUJaqLVfmEIyOAQOIeX_AiT9LNtTH88TI0jE8pityEsxG4B7y7oaeRheAjMkX9r-FUJi08ayh4w-IkD9-r49wzX0IosuwDSf9KQN-wN238xayaIlaXlKXV3zinwL5uMqYMES7NwNHqBOAeG7ffC8SJ6DFNyIQnA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Note that if you do not have administrator rights, you will get an error as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/kJRclFWm8rF2G2egrdaAp3S5QKZSoKcbpaBhkttz0rzf-zbg3ODJY11kpt7izAKxg7PNWKd7BWILtZMmgMd3hFgxgGwGsPa-eENVshUJT2oPXC2wpyvf0a6IaCkUKq2HVECeie1KyeeRldF1LmUSJeJQJAsFP9zNvPn5Z_BnmbF-Yupscqd-qv7H-NbPFcUqFFY5Vfm4RA\" alt=\"\"\/><\/figure>\n\n\n\n<p>To avoid that error, you can remove the highlighted part from the code base.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/-2cG-D_hpd9yJho4ZbS-spnfj3QgyteZ6epjMujjMxaP_dUZ03JAcuh7cEajtuRPXR6vphmTMGiwUJclaJmfSgNWARyD85Y7i9Pw6HtDJy8vXRfxByxJ5x1t3zW6i6xPix68glBf3BFvPxNENsJ78YDBzVbTho7YVmupkW2kdLy3ZhTq8Kah-P5QmFbANNL7sY6OXH66PQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>The only difference is that using the below command will send multiple packets while executing the ping command.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/YXHTT60_tBP4isATLD8nVRyP7Rc9JSBvMI7epCGlqjyc3XRMySMk94enki7btiRGLlsEqekMbtMfNjzYMpWwto0rfIMNLP5HUeS4e7-C50PK4zliVYbEVjOAMcPmGOAFmfSA9G1MCqUWYR-w3sNNykg68uyw5qbRW9eLLGgnXmWGBtYqzwv2of7r1Bi6Q-jSCqsyYKUQgQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run the codebase using the below command.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/a0HNbkOsSkejLNW3-lSg0n3mZrCO4HCTt48R1EZ7YvBsUJaqLVfmEIyOAQOIeX_AiT9LNtTH88TI0jE8pityEsxG4B7y7oaeRheAjMkX9r-FUJi08ayh4w-IkD9-r49wzX0IosuwDSf9KQN-wN238xayaIlaXlKXV3zinwL5uMqYMES7NwNHqBOAeG7ffC8SJ6DFNyIQnA\" alt=\"\"\/><\/figure>\n\n\n\n<p>On success, if it is a valid domain, you will see the following in the console.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/lAtVPrl9R6-Q-kh5UryHpsL0umjz6iXPtWnXK3dQ6dRvub997HwbdBiqbwrbFhFCGfUtwWAW9YNRu8wJ55wz2FostmwrraTqUucPPlV5_jkDf3rz5pUYgenm-90igI2J8kE6df5qG2CLPythdJI4_uKM1d_A82Ua7KbgwtenfCCLqcYdSDsbPddaqfyLzccZVO8XV2kbwg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, let&#8217;s change the code passing an invalid domain.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/weVGf0iAT7zUK8iXaPiKUzo3aE1g8E--pA154mHZo3gNOytHck6JV4976IvAM8xh5HIpCb_TWu6DUgEKjmavfKV9OPDhCoaIlBWun5915Gp9pOVjpXAZKZCf9jYXQ5HwaYlKrt-U4zWFSE-QBLTwjuK0fbsuJtplXEN80cn4BTaluVJdp-lNGner6veS9hVYCohvI2suqw\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run the code using the below command.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/a0HNbkOsSkejLNW3-lSg0n3mZrCO4HCTt48R1EZ7YvBsUJaqLVfmEIyOAQOIeX_AiT9LNtTH88TI0jE8pityEsxG4B7y7oaeRheAjMkX9r-FUJi08ayh4w-IkD9-r49wzX0IosuwDSf9KQN-wN238xayaIlaXlKXV3zinwL5uMqYMES7NwNHqBOAeG7ffC8SJ6DFNyIQnA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Since it was an invalid domain, it displays the below result.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/Vrj8Ij5WVd8eIN4A1UgBLLv8gOhDHo3WDzgjgwauyDH9rcg62M7MBZha5nwPKGMkhmeQgZtax7yLdRM3VxvOypNolJn-IVEt8eeMZKGfa4v8gUnm9CYRZqd29gUATUNRk0QzUs5qBt11PrgDaIxamGotPaGj6gVDpSrBBtC-q--1FVKOaleTCLu2379_oAegLGhPfvaBqA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, since we created a txt file with some domains in it, we will open that file and read the domains in it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/5FT8DY2bciMSjqfi2Oht4g1bb_oUdKbkdpbnEwaAMsS9GAyXAeswXu7PZMdQIezV6NUrNaBDBHcLuaoHVtmlIp3jWZhnBrLGH-fPASCzB3rCWFn80k9UuBumkTQnb18i7NLLv-b6OxmNqykEPeUhqa-7W_2DiK1eqFEzqBEvVh-VcG9JJQEaWWe7v-j6OuOYuIJOLfrhrA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Execute the codebase.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/a0HNbkOsSkejLNW3-lSg0n3mZrCO4HCTt48R1EZ7YvBsUJaqLVfmEIyOAQOIeX_AiT9LNtTH88TI0jE8pityEsxG4B7y7oaeRheAjMkX9r-FUJi08ayh4w-IkD9-r49wzX0IosuwDSf9KQN-wN238xayaIlaXlKXV3zinwL5uMqYMES7NwNHqBOAeG7ffC8SJ6DFNyIQnA\" alt=\"\"\/><\/figure>\n\n\n\n<p>On success, you will see the list of servers in the txt file in an array format.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/II1hf4sdMj1zxaoaCcAL-E8LVwav9PcTsqU_QXrLcHtSdHEs9mzYm8C09ictN44e6qNoy8UUB5XRwDyXYB0X89cHyM997r4XdAzX9Z3PSX0J1T8AZA3OMfNzFamv_pDHbb1Wvxb_t292xQJ76Me8TgJqUa6Z3bsoBlQjfsgtaWXAvLPLjBOUlpMTGOCvxlvm64o4tB3THQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Use the below code to perform a strip operation through all the servers.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/4Io5_f2ca4pbwE3V_ohK-hXv1j0bejzypGRokd3sYPTWbNTuFwXai4eU0txDtT7olRydHwo4-4UzB3VuTEHCbz2lV5FbTXrUbgyx-DtduVfqIDXdUB173xXmeXTRFEoMC64EFJQ2y_fXBlZ3IlGjukW6xtL5nMdnLqHv19PpZez_GAuloA3zeFVEwCtg10id7bTgHiXvKg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run the code using the below command.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/a0HNbkOsSkejLNW3-lSg0n3mZrCO4HCTt48R1EZ7YvBsUJaqLVfmEIyOAQOIeX_AiT9LNtTH88TI0jE8pityEsxG4B7y7oaeRheAjMkX9r-FUJi08ayh4w-IkD9-r49wzX0IosuwDSf9KQN-wN238xayaIlaXlKXV3zinwL5uMqYMES7NwNHqBOAeG7ffC8SJ6DFNyIQnA\" alt=\"\"\/><\/figure>\n\n\n\n<p>You will get to see the servers in an array format.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/CZXxZoXfyDDFCKlQF_B5-If8e_pEMn-o9sSI1P-egpSIpExX_zJGgVNkIJm3c_SBoFeatNZoUf4kO4AsgLsqbQgRrEgLCWq03v_lZoZjeyCqRzAXFat3RNay9tZYFshazlORcUG9tGP0FnNGHi2d_q4jURvVMiO6c3LeyXvb_kxJcgcxpAwGTtJhh_PrHyFadkx7WJ1usQ\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">Now, finally, call the execute command function that we created above passing the servers list as the parameter, and print the final output.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/7LSBIhU9i-f0r04UrA7GwuXhKvVvt-G3z0Wvcf6edjPL8xspnqx6ayyeKnANQv8imMZrBk3g2tTzX3csYAj_i2Ob6khphLhNEwEYUjcVrlg2yjdjfEfm7-UhgKPWdaqUFx-4qyYXz3isT2BpwbWgWDGwbuT82ax-OR7JTT03-cD1o49LzL7eoilrsp5T5mUp2XY5qCGRcQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Make the changes as follows in the function code adding a loop to iterate over all servers in the file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/fqKNWIRA1ADi-oFZor_jL53AQ3CWk2GNYUaLYvLpI3x1EAupPiKENxGxpiH6h46KlTFUeC6O0f_n6ClGYu2rELwiv8IwkaK96LcoaS2n0zxq73y37Dhw8sOKKDwtHNg6x-GBTpF0zovPB9Jw-7fobR_m2gvn_26Jn70lFOhouwKyqxDPrQqi9FWKrpLn9eLfqoPDpsa6Lg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run the codebase using the below command.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/a0HNbkOsSkejLNW3-lSg0n3mZrCO4HCTt48R1EZ7YvBsUJaqLVfmEIyOAQOIeX_AiT9LNtTH88TI0jE8pityEsxG4B7y7oaeRheAjMkX9r-FUJi08ayh4w-IkD9-r49wzX0IosuwDSf9KQN-wN238xayaIlaXlKXV3zinwL5uMqYMES7NwNHqBOAeG7ffC8SJ6DFNyIQnA\" alt=\"\"\/><\/figure>\n\n\n\n<p>You will see the successful execution of the ping command for all the servers.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/dpAneMjeSPVgX1jCl_JbgBdIEmnp9QlpYERoFOBmrLgJpFbKH3ZyNMfRdqmIWl53HeY7rhESAhF1Jiexis0UNLn90X1QpiNLJOeWCCHsCp741fWdaNOkmdXWMiQ_jOvShmqbgITCyCAyHgmJsPvit987qHB-Gczyr0pUCP0J1Dgq30kkQRBlQ1ZhCvkgNPAO22nBLsPzRw\" alt=\"\"\/><\/figure>\n\n\n\n<p>Enter a combination of valid and invalid servers in the <em>server.txt<\/em> file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/T9mAoD3SizVBSbafhjnygSHIQ6twIu_-Ob099p2E5VU_MiBmSe6vj9c8e3B4ztl8YR1NA-jQpILFFi2_r2a_JAp0sniV026HyjiRX4s4k3K3ogNykYc_jDyLqoQoyGU9qKTJHQOJEBckvYlWwOqShHpNtVay8jjU7TdiWZJMXxvj2nlv_fVww-SnDkUsQG3QakzjTOCAAA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run the server again.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/a0HNbkOsSkejLNW3-lSg0n3mZrCO4HCTt48R1EZ7YvBsUJaqLVfmEIyOAQOIeX_AiT9LNtTH88TI0jE8pityEsxG4B7y7oaeRheAjMkX9r-FUJi08ayh4w-IkD9-r49wzX0IosuwDSf9KQN-wN238xayaIlaXlKXV3zinwL5uMqYMES7NwNHqBOAeG7ffC8SJ6DFNyIQnA\" alt=\"\"\/><\/figure>\n\n\n\n<p>On success, you will see the valid output of the ping command results.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/atB-Fs66WpWfqoCKaMlWJjJhqVKSqoYXqObI2CB-8OMcYhePgr8swX9VodIxLZVeILHgKfmChnl-Dzg8dPcFhsWFDLTB_9KaGX2W6SesrV1LHOgFkOJkkmX9q_5ZxkZY3fieYRtsm44HWyHvJvm038oXs-LuLVBDKAOJEPHtJDK9HruCH_t3df1vZzNoJZnfCdz5pvo_Uw\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p>For the final testing, enter 2 invalid servers.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/W77HiRGMo1AC_dhEOpeQIBrKWICVjqCqsda6JuWuycoHMBiM_tN551ydnZHWTMZ7H0XpkQEMHShQ7GslkfZCuTGDNbIh1pbLcFY0fUv4KWReo2o8oU5JpdebOjqM1JiwyIKelqnKR7Hd6i91tUfJ0e0OzNxkS-bd5r488bU8GuqQXa82r_UYf_c8iMCvFqLnznj8XjU8kw\" alt=\"Execute Linux Commands in Python\"\/><\/figure>\n\n\n\n<p>Run the codebase again.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/a0HNbkOsSkejLNW3-lSg0n3mZrCO4HCTt48R1EZ7YvBsUJaqLVfmEIyOAQOIeX_AiT9LNtTH88TI0jE8pityEsxG4B7y7oaeRheAjMkX9r-FUJi08ayh4w-IkD9-r49wzX0IosuwDSf9KQN-wN238xayaIlaXlKXV3zinwL5uMqYMES7NwNHqBOAeG7ffC8SJ6DFNyIQnA\" alt=\"\"\/><\/figure>\n\n\n\n<p>You will see the successful result for the following execution.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/sAQuNB2XzvNuVKLOSOH_UmQBwHLSzzEPRBYa1XrB3eXV_YmoJErBYSE_5B9HaRVXf8Q_3WWuOH1CsyY77UqzT-z1ie7hE75zCr4UBvrrnIMz6GHQmEpvFsTuNzTOmKrj2ihjCRhsSGRdQS-YNWg_B2D7nqmeYQ2lTPMR1w9qfTE6E6OGOGnX-YFlNLon1TE9_CcDmwtkJA\" alt=\"\"\/><\/figure>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">In this hands-on, we saw how we can execute the ping Linux command in Python. We began with the creation of an empty directory. Creating a text file, we stored several valid server domains within the file. Then we installed a package if it didn&#8217;t exist. Moving further, we added the code creating a new file for the code. We then altered the code based on whether or not we had administrator rights. Post that we ran the server and tested out the execution for a single valid server followed by testing out the same for an invalid server. Altering the code, we then executed the codebase onto a list of valid servers. On success, we changed the server list file to fill in valid and invalid servers. Post testing the same, finally we passed in only invalid servers to test out the code. We will come up with more such use cases in our upcoming blogs.<\/p>\n\n\n\n<p><strong>Meanwhile\u2026<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">If you are an aspiring Python Developer and want to explore more about the above topics, here are a few of our blogs for your reference:<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-connect-snowflake-with-python-and-execute-queries\/\">How to connect Snowflake with Python and execute queries?<\/a><\/li><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-easily-build-etl-pipeline-using-python-and-airflow\/\">How to easily build ETL Pipeline using Python and Airflow?<\/a><\/li><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-send-webhooks-using-python-and-receive-via-node-js-applications\/\">How to send Webhooks using Python and receive via Node.js Applications?<\/a><\/li><li><a href=\"https:\/\/www.workfall.com\/learning\/blog\/how-to-build-a-web-scraper-using-python\/\">How to build a Web Scraper using Python?<\/a><\/li><\/ul>\n\n\n\n<p>Stay tuned to get all the updates about our upcoming blogs on the cloud and the latest technologies.<\/p>\n\n\n\n<p><strong>Keep Exploring -&gt; Keep Learning -&gt; Keep Mastering&nbsp;<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">At <a href=\"https:\/\/www.workfall.com\/\">Workfall<\/a>, we strive to provide the best tech and pay opportunities to kickass coders around the world. If you\u2019re looking to work with global clients, build cutting-edge products and make big bucks doing so, give it a shot at <a href=\"https:\/\/www.workfall.com\/partner\/\">workfall.com\/partner<\/a> today!<\/p>\n\n\n\n<p><\/p>\n\n\n<style type=\"text\/css\"><\/style><section id='' \n                class='helpie-faq accordions faq-toggle open-first groupSettings-511__enabled' \n                data-collection='' \n                data-pagination='0' \n                data-search='0' \n                data-pagination-enabled='0'\n                role='region'\n                aria-label='FAQ Section'\n                aria-live='polite'><h3 class=\"collection-title\">Frequently Asked Questions:<\/h3><article class=\"accordion \"><div class='helpie-faq-row'><div class='helpie-faq-col helpie-faq-col-12' ><ul><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2887\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2887\"\r\n                data-id=\"post-2887\" \r\n                data-item=\"hfaq-post-2887\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. What does \u201cexecuting Linux commands in Python\u201d mean here?<\/div><\/div><div id=\"accordion-content-post-2887\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2887\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\"><strong>\u00a0A:<\/strong> It means using Python scripts to run shell (Linux) commands \u2014 for example, using the <\/span><span style=\"font-weight: 400\">subprocess<\/span><span style=\"font-weight: 400\"> module \u2014 so that Python can invoke commands like <\/span><span style=\"font-weight: 400\">ping<\/span><span style=\"font-weight: 400\">, file operations, etc., as if typed in a terminal. The blog walks through examples like pinging servers listed in a file.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2888\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2888\"\r\n                data-id=\"post-2888\" \r\n                data-item=\"hfaq-post-2888\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. Which Python modules\/tools are used for this?<\/div><\/div><div id=\"accordion-content-post-2888\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2888\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\"><strong>A:<\/strong> Primarily the built-in <\/span><span style=\"font-weight: 400\">subprocess<\/span><span style=\"font-weight: 400\"> module. The article also refers to reading from files, looping through lists, converting outputs to strings, and handling administrator privileges.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2889\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2889\"\r\n                data-id=\"post-2889\" \r\n                data-item=\"hfaq-post-2889\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. Are there security risks?<\/div><\/div><div id=\"accordion-content-post-2889\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2889\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\"><strong>A:<\/strong> Yes. When executing shell commands from Python, risks include shell injection (if user input is passed unfiltered), executing commands with elevated privileges accidentally, or exposing sensitive data. The article doesn&#8217;t go deeply into security hardening, but in practical use you must validate\/escape inputs and avoid passing untrusted data to <\/span><span style=\"font-weight: 400\">subprocess<\/span><span style=\"font-weight: 400\"> especially with <\/span><span style=\"font-weight: 400\">shell=True<\/span><span style=\"font-weight: 400\">.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2890\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2890\"\r\n                data-id=\"post-2890\" \r\n                data-item=\"hfaq-post-2890\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. What about permissions \/ administrative rights?<\/div><\/div><div id=\"accordion-content-post-2890\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2890\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\"><strong>A:<\/strong> Some Linux commands require root or sudo privileges. The article points out that if you don\u2019t have administrator rights, certain commands may fail, and shows alternatives or modifications to avoid those parts.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-2891\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-2891\"\r\n                data-id=\"post-2891\" \r\n                data-item=\"hfaq-post-2891\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. What are potential portability issues?<\/div><\/div><div id=\"accordion-content-post-2891\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-2891\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\"><strong>\u00a0A:<\/strong> Some Linux commands behave differently across distributions; Windows may handle commands differently or not support certain commands at all. Also the path, environment variables, permission models vary. If writing cross-platform tools, be aware of these differences. The article assumes a Linux environment.<\/span><\/p>\n<\/div><\/li><\/ul><\/div><\/div><\/article><\/section>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span> As of this writing, Linux has a global desktop market share of 2.77% (A Report by Statcounter), but it powers over 90% of all cloud infrastructure and hosting services. It is critical to be familiar with common Linux commands for this reason alone. According to a 2022 StackOverflow survey, Linux-based operating systems are more popular [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1538,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[288],"tags":[4,142,12,114,377,225,6],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Execute Linux Commands in Python? - The Workfall Blog<\/title>\n<meta name=\"description\" content=\"As of this writing, Linux has a global desktop market share of 2.77%. It is critical to be familiar with common Linux commands for this...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Execute Linux Commands in Python? - The Workfall Blog\" \/>\n<meta property=\"og:description\" content=\"As of this writing, Linux has a global desktop market share of 2.77%. It is critical to be familiar with common Linux commands for this...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"The Workfall Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/workfall\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-27T09:52:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-23T10:13:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1-3.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@workfall\" \/>\n<meta name=\"twitter:site\" content=\"@workfall\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Workfall\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\",\"name\":\"Workfall - Hire #Kickass Coders On Demand\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/workfall\/\",\"https:\/\/www.linkedin.com\/company\/workfall\/\",\"https:\/\/facebook.com\/workfall\",\"https:\/\/twitter.com\/workfall\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400\",\"contentUrl\":\"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400\",\"width\":400,\"height\":400,\"caption\":\"Workfall - Hire #Kickass Coders On Demand\"},\"image\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#website\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/\",\"name\":\"The Workfall Blog\",\"description\":\"#Tech #Remote #Jobs\",\"publisher\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learning.workfall.com\/learning\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#primaryimage\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1-3.png\",\"contentUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1-3.png\",\"width\":1200,\"height\":628,\"caption\":\"How to Execute Linux Commands in Python?\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#webpage\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/\",\"name\":\"How to Execute Linux Commands in Python? - The Workfall Blog\",\"isPartOf\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#primaryimage\"},\"datePublished\":\"2022-12-27T09:52:11+00:00\",\"dateModified\":\"2025-09-23T10:13:47+00:00\",\"description\":\"As of this writing, Linux has a global desktop market share of 2.77%. It is critical to be familiar with common Linux commands for this...\",\"breadcrumb\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/learning.workfall.com\/learning\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Execute Linux Commands in Python?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#webpage\"},\"author\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a\"},\"headline\":\"How to Execute Linux Commands in Python?\",\"datePublished\":\"2022-12-27T09:52:11+00:00\",\"dateModified\":\"2025-09-23T10:13:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#webpage\"},\"wordCount\":1363,\"publisher\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1-3.png\",\"keywords\":[\"Cloud\",\"linux\",\"macOS\",\"python\",\"pythondevelopers\",\"pythonprogramming\",\"workfall\"],\"articleSection\":[\"Backend Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a\",\"name\":\"Workfall\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png\",\"contentUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png\",\"caption\":\"Workfall\"},\"sameAs\":[\"https:\/\/www.workfall.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Execute Linux Commands in Python? - The Workfall Blog","description":"As of this writing, Linux has a global desktop market share of 2.77%. It is critical to be familiar with common Linux commands for this...","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:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Execute Linux Commands in Python? - The Workfall Blog","og_description":"As of this writing, Linux has a global desktop market share of 2.77%. It is critical to be familiar with common Linux commands for this...","og_url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/","og_site_name":"The Workfall Blog","article_publisher":"https:\/\/facebook.com\/workfall","article_published_time":"2022-12-27T09:52:11+00:00","article_modified_time":"2025-09-23T10:13:47+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1-3.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_creator":"@workfall","twitter_site":"@workfall","twitter_misc":{"Written by":"Workfall","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization","name":"Workfall - Hire #Kickass Coders On Demand","url":"https:\/\/learning.workfall.com\/learning\/blog\/","sameAs":["https:\/\/www.instagram.com\/workfall\/","https:\/\/www.linkedin.com\/company\/workfall\/","https:\/\/facebook.com\/workfall","https:\/\/twitter.com\/workfall"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400","contentUrl":"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400","width":400,"height":400,"caption":"Workfall - Hire #Kickass Coders On Demand"},"image":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#website","url":"https:\/\/learning.workfall.com\/learning\/blog\/","name":"The Workfall Blog","description":"#Tech #Remote #Jobs","publisher":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learning.workfall.com\/learning\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#primaryimage","url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1-3.png","contentUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1-3.png","width":1200,"height":628,"caption":"How to Execute Linux Commands in Python?"},{"@type":"WebPage","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#webpage","url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/","name":"How to Execute Linux Commands in Python? - The Workfall Blog","isPartOf":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#primaryimage"},"datePublished":"2022-12-27T09:52:11+00:00","dateModified":"2025-09-23T10:13:47+00:00","description":"As of this writing, Linux has a global desktop market share of 2.77%. It is critical to be familiar with common Linux commands for this...","breadcrumb":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learning.workfall.com\/learning\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Execute Linux Commands in Python?"}]},{"@type":"Article","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#article","isPartOf":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#webpage"},"author":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a"},"headline":"How to Execute Linux Commands in Python?","datePublished":"2022-12-27T09:52:11+00:00","dateModified":"2025-09-23T10:13:47+00:00","mainEntityOfPage":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#webpage"},"wordCount":1363,"publisher":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization"},"image":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-execute-linux-commands-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1-3.png","keywords":["Cloud","linux","macOS","python","pythondevelopers","pythonprogramming","workfall"],"articleSection":["Backend Development"],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a","name":"Workfall","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/image\/","url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png","contentUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png","caption":"Workfall"},"sameAs":["https:\/\/www.workfall.com"]}]}},"jetpack_featured_media_url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/12\/Cover-Images_Part2-1-3.png","jetpack-related-posts":[{"id":1348,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-snowflake-with-python-and-execute-queries\/","url_meta":{"origin":1537,"position":0},"title":"Connect Snowflake with Python and execute queries","date":"September 27, 2022","format":false,"excerpt":"Snowflake cloud data warehouse is a buzzing trend in managing data these days as it has advantages like cost-effectiveness, auto-scaling, easy-to-transform data, etc. over traditional data warehouses. It is greatly assisting organizations in terms of its critical role in ELT (Extract-Load-Transform). Python is a very popular programming language that is\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"Connect Snowflake with Python and execute queries","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/09\/Cover-Images_Part2-1-3.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":169,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-launch-and-connect-to-a-macos-instance-on-amazon-ec2-and-access-it-through-a-gui-using-vnc-client\/","url_meta":{"origin":1537,"position":1},"title":"How to launch and connect to a MacOS instance on Amazon EC2 and access it through a GUI using VNC Client?","date":"October 27, 2021","format":false,"excerpt":"Amazon EC2 MacOS Instances allow to execution of on-demand MacOS workloads in the cloud, expanding AWS' flexibility, scalability, and cost advantages to all Apple developers. Developers working on apps for the iPhone, iPad, Mac, Apple Watch, Apple TV, and Safari may use EC2 MacOS Instances to provision and access MacOS\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"Launch & Connect to a MacOS instance on Amazon EC2","src":"https:\/\/i1.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2021\/10\/EC2MacInstance-1200-x-628-px.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1126,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-easily-build-etl-pipeline-using-python-and-airflow\/","url_meta":{"origin":1537,"position":2},"title":"Easily build ETL Pipeline using Python and Airflow","date":"August 16, 2022","format":false,"excerpt":"Apache Airflow is an open-source workflow management platform for authoring, scheduling, and monitoring workflows or data pipelines programmatically. Python is used to write Airflow, and Python scripts are used to create workflows. It was created by Airbnb. In this blog, we will show how to configure airflow on our machine\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"ETL Pipeline using Python and Airflow","src":"https:\/\/i1.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/08\/Cover-Images_Part2-1-2.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2429,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-an-amazon-price-tracker-service-using-python\/","url_meta":{"origin":1537,"position":3},"title":"How to Create an Amazon Price Tracker Service Using Python?","date":"August 29, 2023","format":false,"excerpt":"Hey there, shopping savvy! Ever wished you could magically know when your favorite Amazon items go on sale? Guess what \u2013 we've cracked the code!\u00a0 Learn how to build your very own Amazon Price Tracker using Python. Imagine getting alerts right in your inbox when prices drop. Let's dive in\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"Amazon Price Tracker Service","src":"https:\/\/i2.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/08\/Amazon-Price-Tracker-Service.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2388,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-read-and-write-in-google-spreadsheet-using-python-and-sheety-api\/","url_meta":{"origin":1537,"position":4},"title":"How to Read and Write In Google Spreadsheet Using Python and Sheety API?","date":"July 25, 2023","format":false,"excerpt":"Tired of manual data entry in Google Spreadsheets? Discover a simple and efficient way to automate your data handling using Python and Sheety API. In this blog, we'll demonstrate step-by-step the process of reading and writing data in Google Sheets, empowering you to effortlessly manage your data with the power\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"Read and Write In Google Spreadsheet Using Python and Sheety API","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/07\/Cover-Images_Part2-1-3.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2448,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-stream-json-data-using-server-sent-events-and-fastapi-in-python-over-http\/","url_meta":{"origin":1537,"position":5},"title":"How to Stream JSON Data Using Server-Sent Events and FastAPI in Python over HTTP?","date":"September 26, 2023","format":false,"excerpt":"In this blog, we will cover: What are Server-Sent Events?Why Stream Data Using Server-Sent Events (SSE)?What is FastAPI?Hands-OnConclusion What are Server-Sent Events? Server-Sent Events (SSE) is a simple and efficient technology for sending real-time updates from the server to the web browser over a single HTTP connection. Unlike other real-time\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"How to Stream JSON Data Using Server-Sent Events and FastAPI in Python over HTTP?","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/Tech-Blogs-Cover-Images_Part3-1-3.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1537"}],"collection":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/comments?post=1537"}],"version-history":[{"count":4,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1537\/revisions"}],"predecessor-version":[{"id":2893,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/1537\/revisions\/2893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/media\/1538"}],"wp:attachment":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/media?parent=1537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/categories?post=1537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/tags?post=1537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}