Find the best tutors and institutes for Python Training

Find Best Python Training classes

Please select a Category.

Please select a Locality.

No matching category found.

No matching Locality found.

Outside India?

Search for topics

Python in Unix

Rishi B.
03/04/2017 0 0

Python in Unix

How to run command on unix

Commands cannot be run directly on a unix server like we do in shell script.


  1. system("Command")
  2. check_output("Command", shell=True)
  3. call("Command", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)


1.Will print the output on screen, can store this in a variable but additional “0” will come on newline

  1. Output will not come on the screen. Can store it in a variable and print it. However, extra newline character come with it.
  2. It gives status of the command. Whether command was successfully executed or not.




import subprocess

os.system("ls –ltr /tmp”)


Using output from subprocess:

output=subprocess.check_output("command", shell=True)

Caution:the result stored in output is not Boolean so you can’t compare it in if statement. Below code will not work correctly. Also, the output is having change line character in the end.

Below Code will not work



import subprocess

output=subprocess.check_output("grep -i noexec_user_stack_log /etc/system | wc -l", shell=True)

if output == 0:





Correct code is:



import subprocess

exit_code ="grep -i noexec_user_stack_log /etc/system | wc -l", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

if exit_code == 0:





Making it simple by use of function:

from subprocess import PIPE, Popen

def cmdline(command):

    process = Popen(





    return process.communicate()[0]

print cmdline("cat /etc/host")

print cmdline('ls -ltr')

print cmdline('grep "max" /etc/system')

print cmdline('nslookup') 

How to use variable in commands

Simply using a variable inside os.system will not work.



import os,sys
os.system("ls -ltr /tmp")
os.system('chmod 750 /tmp/%(FILE)s'  % locals())


How to assign unix command output to a variable and use it

When we store output from subprocess.check_output in a variable, it also stores newline character with the output. We need to remove it first then we can add any string or variable after that to print in same line.



import subprocess,os,sys

SERVER=subprocess.check_output("hostname", shell=True)



cmd="{}_zpool_fs {}".format(cmd,LUN_ID)

print cmd


0 Dislike
Follow 0

Please Enter a comment


Other Lessons for You

Write your first Python program in 10 minutes
1. Download python from python official site search "python download" in google 2. Install in your machine 3. verify using : "python --version" command 4. Write first program using notepad create...

Debrati Sadhu | 4 days ago

0 0

Why Python
Python can be used in any futuristics technology A= Analytics Data Science Artificial Intelligence(AI) Neural Network(NN) Natural Language Processing(NLP) Computer Vision(OpenCV) In Analytics...

Mujahed | 01 Apr

0 0

Black in Python
When you are upturn in your career from beginner to experienced in programming world, your team will start looking at ‘how you are writing?’ Here the responsibility piling up. Okay,...

Logeswaran | 31 Jan

0 0

Be A Python Dev
Currently, in the programming world, Python is one of the languages with a high rising demand profile. And this article will explain why that isn’t slowing down in the foreseeable future.Python...

URIAH Training Center | 18/12/2019

0 1

All About Python You Need To Know Before Starting
Python for Beginners------Anyone can learn Python Prerequisites to learn Python To learn Python we don’t require the knowledge of any other programming languages. To learn all other programming...

URIAH Training Center | 12/12/2019

1 0

Looking for Python Training Classes?

Find best tutors for Python Training Classes by posting a requirement.

  • Post a learning requirement
  • Get customized responses
  • Compare and select the best

Looking for Python Training Classes?

Find best Python Training Classes in your locality on UrbanPro

Post your learning requirement is India's largest network of most trusted tutors and institutes. Over 25 lakh students rely on, to fulfill their learning requirements across 1,000+ categories. Using, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 6.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more