The main goal for us is how to use this programming language in everyday life with different tasks.
Today I will come up with examples to cover this goal and show you how to use the subprocess python module.
- using the powershell with python :
-
>>> import subprocess
>>> process=subprocess.Popen(["powershell","Get-Childitem C:\\Windows\\*.log"],stdout=subprocess.PIPE);
>>> result=process.communicate()[0]
>>> print result - get and print the hostname :
-
>>> print subprocess.check_output("hostname") - print the output of ping command :
-
>>> print subprocess.check_output("ping localhost", shell=True) - print the output of dir command :
-
>>> cmd = 'dir *'
>>> supcmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> print supcmd.communicate()[0] - run the python script like python shell :
-
>>> import sys
>>> import subprocess
>>> pid = subprocess.Popen([sys.executable, "calc.py"])
No comments:
Post a Comment