Welcome, guest | Sign In | My Account | Store | Cart

Running any linux command line inside your python script.

Python, 12 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#/usr/bin/env python
import subprocess

class RunCmd(object):
    def cmd_run(self, cmd):
        self.cmd = cmd
        subprocess.call(self.cmd, shell=True)

#Sample usage

a = RunCmd()
a.cmd_run('ls -l')

3 comments

Andrey Selemenev 11 years, 7 months ago  # | flag

Bad code. Stop writing classes everywhere!

Vladimir Chupakhin 11 years, 7 months ago  # | flag

import os os.system("ls -l")

to Andrey, for example do you have in your mind?

Ahmed Kamel (author) 11 years, 7 months ago  # | flag

well i meant that i can extend this class to deal with stdout stderr with subprocess module and i think this option is not supported by os module.