Python Forum

Full Version: OS command via python subprocess module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Can we use below OS commands (inside the file.txt) and then pass this file an argument to the subprocess.popen for execution?
OR is there a better way to execute all below OS commands inside python script?





export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$PATH 
cd $ORACLE_HOME 
mv OPatch OPatch.pre6880880 
mkdir OPatch 

 
osTyp=`uname -s` 
if [ "$osTyp" = "Linux" ]; 
then 
cp /gfd/infr/DBA/oracle12102-Linux-X86-64/6880880_zipDir/p6880880_122010_Linux-x86-64.zip . 
unzip p6880880_122010_Linux-x86-64.zip 
elif [ "$osTyp" = "SunOS" ]; 
then 
cp /gfd/infr/DBA/oracle12102-Solaris64/6880880_zipDir/p6880880_122010_SOLARIS64.zip . 
unzip p6880880_122010_SOLARIS64.zip 
else 
echo "!!" echo "!! unable to determine OS type" echo "!!" 
fi
I recently came across a nice way to convert such scripts: the plumbum module. Using this module, your python code could more or less resemble the following
from plumbum import local, cmd
ORACLE_HOME = local.env['ORACLE_HOME']
local.env['PATH'] = "{ohome}/bin:{ohome}/OPatch:{path}".format(
    ohome=ORACLE_HOME, path=local.env[PATH])
local.cwd.chdir(ORACLE_HOME)
cmd.mv('OPatch', 'OPatch.pre6880880')
cmd.mkdir('OPatch')

osTyp = cmd.uname('-s').strip()
if osTyp == 'Linux':
    cmd.cp(
        "/gfd/infr/DBA/oracle12102-Linux-X86-64/6880880_zipDir/p6880880_122010_Linux-x86-64.zip", ".")
    cmd.unzip("p6880880_122010_Linux-x86-64.zip")
elif osTyp == 'SunOs':
    cmd.cp("/gfd/infr/DBA/oracle12102-Solaris64/6880880_zipDir/p6880880_122010_SOLARIS64.zip", ".") 
    cmd.unzip("p6880880_122010_SOLARIS64.zip") 
else
    print("!!\n!! unable to determine OS type !!\n!!")
Thanks. Will look into the plumbum module. Anyways any idea why I m not able to get cd via subprocess.run

subprocess.run('echo $ORACLE_HOME', shell=True)
subprocess.run('cd "$ORACLE_HOME"', shell=True)
subprocess.run('pwd', shell=True)
output:
/u01/app/oracle/product/12.1.0.2/db_1
/u02/scripts/Patching
(Oct-20-2018, 09:53 AM)alinaveed786 Wrote: [ -> ]any idea why I m not able to get cd via subprocess.run
It is because subprocess.run('cd $ORACLE_HOME', shell=True) starts a new shell process and changes directory in this spawned process. It doesn't change the current directory of the calling process. For this you need to call os.chdir()
Not working either with os.chdir.

#!/usr/bin/python
import os
import subprocess


subprocess.run('echo $ORACLE_HOME', shell=True)

os.chdir("'$ORACLE_HOME'")

subprocess.run('pwd', shell=True)
Error
/u01/app/oracle/product/12.1.0.2/db_1
Traceback (most recent call last):
  File "/u02/scripts/Patching/Ping.py", line 9, in <module>
    os.chdir("'$ORACLE_HOME'")
FileNotFoundError: [Errno 2] No such file or directory: "'$ORACLE_HOME'"
Try os.chdir(os.environ['ORACLE_HOME'])
thanks, it worked. Moreover, I installed the plumbum module and executed the code but got below error
Traceback (most recent call last):
  File "/u02/scripts/Patching/Test.py", line 8, in <module>
    local.env['PATH'] = "{ohome}/bin:{ohome}/OPatch:{path}".format(ohome=ORACLE_HOME, path=local.env[PATH])
NameError: name 'PATH' is not defined
(Oct-20-2018, 10:22 AM)alinaveed786 Wrote: [ -> ]thanks, it worked. Moreover, I installed the plumbum module and executed the code but got below error
Traceback (most recent call last):
  File "/u02/scripts/Patching/Test.py", line 8, in <module>
    local.env['PATH'] = "{ohome}/bin:{ohome}/OPatch:{path}".format(ohome=ORACLE_HOME, path=local.env[PATH])
NameError: name 'PATH' is not defined

The source of you exception (posting code with lines broken to fit screen may be a good idea)
path=local.env[PATH]
Thanks, it worked. Additionally, I want to add below statement to the code but its throwing error. Basically, want to remove directory "OPatch.pre6880880" but it's not an empty directory

#!/usr/bin/python

from plumbum import local, cmd

ORACLE_HOME = local.env['ORACLE_HOME']
path=local.env['PATH']
local.env['PATH'] = "{ohome}/bin:{ohome}/OPatch:{path}".format(ohome=ORACLE_HOME, path=local.env['PATH'])
local.cwd.chdir(ORACLE_HOME)
cmd.rm -rf('OPatch.pre6880880')
cmd.mv('OPatch', 'OPatch.pre6880880')
cmd.mkdir('OPatch')
osTyp = cmd.uname('-s').strip()

if osTyp == 'Linux':
    cmd.cp("/u03/p6880880_121010_Linux-x86-64.zip", ".")
    cmd.unzip("p6880880_121010_Linux-x86-64.zip")
elif osTyp == 'SunOs':
    cmd.cp("/u03/p6880880_121010_Linux-x86-64.zip", ".")
    cmd.unzip("p6880880_121010_Linux-x86-64.zip")
else:
    print("!!\n!! unable to determine OS type !!\n!!")
Error
    cmd.rm -rf('OPatch.pre6880880')
NameError: name 'rf' is not defined
This is python, not bash. Use cmd.rm('-rf', 'OPatch.pre6880880' )
Pages: 1 2 3