Open main menu

CDOT Wiki β

Changes

OPS435 Python3 Lab 3

2,144 bytes added, 17:27, 19 September 2019
PART 2 - Running System Commands with subprocess
os.system('whoami')
os.system('ifconfig')
</source>Notice that the output from the system programs '''ls''', '''whoami', and '''ifconfig''' is printed in on your scriptscreen. Consider that may not always be what you wantTry to compare them with the output from the following function calls:<source lang="python">ls_return = os.system('ls')print('The contents of ls_return:',ls_return)whoami_return = os.system('whoami')print('The contents of whoami_return:',whoami_return)ifconfig_return = os.system('ifconfig')print('The contents of ifconfig_return:',ifconfig_return)<br/source>Does the os.system() function return something? If it does, what does the return value represent?<br>
:#Try this also:<source lang="python">
ipconfig_return = os.system('ipconfig')print('The contents of ipconfig_return:',ipconfig_return)</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 always a good idea to run system commands in Pythonby calling the os.system() function, this makes your Python code less portable and makes it require a specific operating system or a system that has those commands available.  '''Perform the Following Steps:''':#The os module has another function called '''popen()''' which is capable of launching a Linux command as a process and capture its output and then return it the the caller.:#Create a new python file for testing.:#Import the '''''os''''' module in your python file.:#You should think about can launch a Linux commands by using the '''os.popen()''' function. Try it:<source lang="python">import osos.popen('ls')os.popen('whoami')os.popen('ifconfig')</source>Notice that when there is no observable output from running the Python script listed above. Try to compare it with the output from the following function calls:<source lang="python">import osls_return = os.popen('ls')print('The contents of ls_return:',ls_return)whoami_return = os.popen('whoami')print('The contents of whoami_return:',whoami_return)ifconfig_return = os.popen('ifconfig')print('The contents of ifconfig_return:',ifconfig_return)</source>Does the os.popen() function return something? If it does, what does the return value represent?<br>:#Try this also:<source lang="python">import oswhoami_return=os.popen('whoami')whocmi_contents = whoami_return.read()print('whoami_contents:',whoami_contents)</source>What conclusion would you decide whether you should or should not use a system command to accomplish some task or stick to pure Python code onlydraw from the about python script?:#Try another one:<source lang="python">ipconfig_return = os.popen('ipconfig')print('The contents of ipconfig_return:',ipconfig_return)</source><br><br>As you may recall from lab2, you issued '''import sys''' to import special variables object from the systemthat built-in module. You can also import the subprocess module to load a subprocess in order function called '''Popen()''' which will allow you to run common non OS specific have better control of the output produce by Linux commands securelylike 'ls', 'whoami', and 'ifconfig', etc.<br><br> 
:#Import the subprocess module in your python file.
:#There are many features available as part of the subprocess module, we are interested in "'''Popen()'''"function. This method function subprocess.Popen() can be used to run system commands as a child process to the your Python script. The code below output will create a new child processobject, in which we assign the name '''p'''. In Python we can control the behaviour of this new process through the new Python object we just created, "'''p'''". "'''p'''" now has a collection of methods(functions that are apart of a an object) available.<br><br>
:#To demonstrate, issue the following:<source lang="python">
p = subprocess.Popen(['date'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
:'''Perform the Following Instructions:'''
<ol>
<li>Create the "'''~/ops435/lab3/lab3d.py'''" script. The purpose of this script is to create a Python function that can return the linux free disk space on a Linux system's root directory free space.:*The script should contain a comment line with "Author ID: [seneca_id]"
:*The script should '''import the correct module'''
:*The script should use launch the linux Linux command: '''<nowiki>df -h | grep '/$' | awk '{print $4}'</nowiki>''' as a new process
:*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 https://rawict.githubusercontentsenecacollege.comca/Seneca-CDOT~raymond.chan/ops435/masterlabs/LabCheckScripts/CheckLab3.py
python3 ./CheckLab3.py -f -v lab3d
</source></li>
1,760
edits