Python 2.7 Run linux command and get output














































Python 2.7 Run linux command and get output



This is one of the common requirement to run linux command and get output in the variable. Here is the sample code to achieve it. You need to import subprocess library for this and call the Popen method of subprocess.

Python 3.5+, subprocess library is having the support of run() method which is using to run linux command and get output.

Program:

import sys
import subprocess
command = "ls -lh"
sys.stdout.flush()
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print ("output = {}".format(output))
print ("type(output) = {}".format(type(output)))

Output:

pankaj@pankaj-VirtualBox:~/workspace$ python test.py 
output = total 448K
-rw-rw-r-- 1 pankaj pankaj  138 Sep 25 14:46 access_file.c
-rwxrwxr-x 1 pankaj pankaj 9.5K Feb 19 11:02 a.out
-rw-rw-r-- 1 pankaj pankaj  159 Jul  5  2018 atomicOperation.cc
-rw-rw-r-- 1 pankaj pankaj 3.4K Jan  5 12:50 calendar.cpp
-rwxrwxr-x 1 pankaj pankaj 8.4K Jan 23 14:38 ccc
-rwxrwxr-x 1 pankaj pankaj  11K Oct 10 12:46 client
drwxrwxr-x 3 pankaj pankaj 4.0K Sep  9 20:50 client-servers
-rw-rw-r-- 1 pankaj pankaj 3.7K Jul 11  2018 compare_version.cpp
-rw-rw-r-- 1 pankaj pankaj 1.5K Sep 23 22:38 delete_elements_from_vector.cpp

type(output) = <type 'str'>
pankaj@pankaj-VirtualBox:~/workspace$ 


More Articles of Pankaj Choudhary:

Name Views Likes
Coding Challenges: Fun Projects for Student Programmers 18 0
5 Must-Have Programming Skills for Student 25 0
Hands-On Experience: Internships and Capstone Projects in Data Science Programs 285 0
5 Tips for Effective Self-Study in Programming Education 156 0
5 Essential Study Techniques for Mastering C++ 215 0
What You Need to Know about Programming Languages 164 0
Pitfalls of Learning Programming 178 0
Truminds 5G UPF supports MPTCP Proxy 9965 214
C++ Assignment Help: Where Can You Find? 491 0
Top Benefits of Learning Coding in College or University 430 0
C++ Abstract Class 860 21
Compile C++11 program using g++ 4151 10
C++ Searching an element in a vector 786 11
Priority Queue 1830 12
Check String is palindrome or not 1255 10
Preorder Tree Traversal with recursion 1148 12
Template based linked list 5606 11
Print all processes running as root 864 28
C++ Queue of objects 4913 18
How to cin to vector 17823 10
Python List Built-In Methods 631 11
Python Basic Programs 587 1
C++ Compare version string 3122 12
C++ Vector of objects 6319 20
Windows enable NTP Server 888 17
Tokenized securities on blockchain 747 0
Difference between remove and remove_if in STL 1046 19
CPU usage per thread without top utility 2988 16
Python Selected Interns (Feb 2019 Batch - 1) 1049 24
Complex Number Program 905 10
Functor with arguments 1338 10
Pass function pointer to a function 540 11
C++ Two dimensional vector 947 21
C++ challange for Aug 2019 Interns 6043 4
C program to get memory usage 9776 18
Python challange for Interns (Feb 2019, Batch 2) 878 13
C++ Read file line by line 916 14
Dynamic Programming - Egg Dropping Problem 1762 16
GDB Print complete string 3138 24
UDP Server Program in C 9193 24
C++ Split string by delimiter and store it in vector 3239 12
Python programs list 788 23
Mutable lambda in C++ 4714 11
C++ Sorting of vector 632 10
Overload Assignment Operator 596 10
Blockchain The Benefits and Risks of Smart Contracts 784 0
Inorder Tree Traversal without recursion 1031 14
Lambdas in C++ 792 11
Inorder Tree Traversal with recursion 814 25
C++ Count maximum number of consecutive ones in a vector 1520 10
C++ Parameter Binding 637 11
Python Converting a String to a List of Words 775 19
Python project to update a doc file conver it to the pdf file and send it to multiple email addresses 1831 29
Test article for interns Python Add Two Numbers 688 12
Ring Buffer 5414 11
Python 2.7 Run linux command and get output 1563 38
C++ Predicate 2285 10
Internship Opportunity at cppsecrets for Nov 2020 batch 5063 1
Find N pairs with smallest sums in two vectors 503 11
C++ Set of objects 852 21
C++ Selected Interns (Feb 2019 Batch - 1) 1148 15
Python challange for Sept 2019 Interns 2520 5
Write your own shared pointer 2486 10
Internship Opportunity at cppsecrets for May 2020 batch 10596 4
C++ Check whether two strings are anagram of each other 3369 12
C++11 Range based for loop 803 11
Erase elements from vector 2119 17
Python send mail 807 22
Git create branch 719 21
ssh Bad owner or permissions 843 0
C++11 decltype 526 12
C++ Read file and store it into integer variable 594 12
Python Tcp Server Example Code 829 12
C++ challange for Oct 2019 Interns 5359 0
Linux - Difference between tty and pts 11081 20
sudo error, is mode 0777, should be 0440 16 10
C++ Config Reader 8760 3
Find maximum product of three numbers 524 11
Python Converting a String to a List of Words based on spaces and alphanumeric characters 973 25
Internship Opportunity at cppsecrets.com 4766 0
C++ map of char* and char* 4596 28
C++11 decltype and typeid 2576 16
Fork system call 879 15
Postorder Tree Traversal with recursion 789 21
How to give paid exam at cppsecrets.com? 1137 0
Functor in C++ 612 11
Internship Opportunity at cppsecrets for UPES Hackathon Participants 752 1
C++11 auto keyword 798 21
C++ Boost Serialization: Serializing primitive data types 55 3
C++ Validate parenthesis pattern 572 10
Dynamic Programming - Rod Cutting Problem 1547 20
Linux Print Process Tree 915 27
Stipend update for the C++ interns of Feb 2019 2462 32
Linux Extract an RPM package without installing it 972 16
Preorder Tree Traversal without recursion 781 16
Internship Opportunity at cppsecrets for March 2020 batch 7956 4
Python challange for Feb 2019 Interns 1090 27
UDP Client Program in C 4142 23
Deep Copy 834 11
Find the Nth largest element in a vector 5451 11
10 Most Frequent Laptop Issues and How to Solve them 448 0
Python Selected Interns (Feb 2019 Batch - 2) 974 21
C++ challange for Interns (Feb 2019, Batch 2) 865 24
Select process by PPID 579 17
C++ typeid operator 3739 21
Python UDP client server example code 5560 29
C++ Abstract classes and pure virtual functions 752 23
fatal error: Python.h: No such file or directory 1419 0
Linux Find out how many file descriptors are opened 724 14
C++ Range based for loop 673 18
Binary Tree Traversal 2951 21
Python Command Line Arguments 993 11
How to install Python Requests Library 691 13
Valgrind complete tutorial 1156 10
Function Pointer 619 10
C++ Is paragraph present in the file 678 22
How to cin to list 7521 10
C++11 Auto Example Code 744 14
Shallow Copy 2032 10
C++ File handling 1483 11
C++ challange for Feb 2019 Interns 1063 11
ps man page 727 19
errno.h Error Codes in Linux 835 15
Write copy constructor, assigment operator and desctructor of class having pointer variable 889 10
popen man page 845 15
C++ Read and write binary file 6224 22
C++ Generic file writer class 1179 22
C++ Object Slicing 636 14
OpenSSL generate hash of the file 1954 22
Singleton Class using C++ 11 4550 12
C++ challange for Interns May 2019 1473 21
C++ Pass local variable to Functor 739 10
Nested Template Class 1169 11
Python Sum of all the items in a list 546 11
How to give internship exam at cppsecrets.com 2547 1
C++ Selected Interns (Feb 2019 Batch - 2) 995 19
Run unix command and get output 3177 28
Python Top MOST famous Python libraries and framework 805 29
Linux - find all the shell/terminals are running 848 29
Python challange for May 2019 Interns 1176 15
Linux Select Process By PPID 817 20
C++ File read and write 586 12
C++ Best way to trim std::string 5354 28
Build-in Functors 556 10
C++ Template with default arguments 737 20
How To Move Forward After Losing Your Job Due To Covid 316 0
Truminds 5G UPF now runs in 6WINDGate as a plugin 12654 2312

Comments