Changes

Jump to: navigation, search

OPS435 Python Lab 3

505 bytes added, 11:35, 14 August 2017
PART 2 - Running System Commands with Subprocess
:#Issue the following in ipython:<source>
system(ipconfig)
</source>You should notice an error message: ''''ipconfig command not found''''. That error occurs since that command was an MS Windows command, and our current platform is Linux.<br><br>It is not usually always a good idea to run system commands in Python, this makes your Python code less portable and makes it require a specific operating system or a system that has those commands available. Also, allowing python to execute commands on the operating system can be a '''security problem'''. For these reasons you should only use '''sub-process''' and '''system commands''' as a last resort and stick to Python code only.<br><br>As you may recall from lab2, you issued '''import sys''' to import special variables from the system. You can import a subprocess in order to run common non OS specific commands securely.<br><br>
:#Issue the following in ipython:<source>
import subprocess
p = subprocess.Popen(['date'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
dir(p)
</source>This function call and the following step is full of details we haven't yet talked about which is why it may look a little scary. By the time we're finished with the course - you will be able to look at code like this and not be intimidated. If you're curious and want to look ahead - you can find the definition for the [https://docs.python.org/3/library/subprocess.html#subprocess.Popen Popen function in the Python reference manual].:#This next step is going to communicate with the process and get the retrieve it's output (stdout and stderr from the command we previously).<source>
output = p.communicate()
output
stdout
</source>
:#While many of these system commands could Sometimes you will be instead written in simply Pythonable to use purely python code to get your job done, the exercise of running but often you will need to call existing system commands is . It's importantto learn how to call them and how to interact with those external processes.
'''Practice Running System Commands From Python'''
:'''Perform the Following Instructions:'''
:#Create the "'''~/ops435/lab3/lab3d.py'''" script. The purpose of this script is to create a Python function that can return the linux system's root directory free space.
:::*The script should have a '''Shebang line''':::*The script should '''import subprocessthe correct module''':::*The script should use the linux command: '''<nowiki>"df -h | grep '/$' | awk '{print $4}'"</nowiki>'''
:::*The script should contain the function called: '''free_space()'''
:::*The function '''free_space()''' should return a string which is in '''utf-8''' and has '''newline characters strip'''
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
ls CheckLab3.py || wget matrix.senecachttps://raw.ongithubusercontent.cacom/~acoatleySeneca-willisCDOT/ops435/master/LabCheckScripts/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3d
</source>

Navigation menu