LoginSignup
0
1

More than 5 years have passed since last update.

Python2 > Python scriptを置いているディレクトリにあるbash scriptを実行する実装 > def invoke_script(ascript)

Last updated at Posted at 2017-10-03
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

Python 2.6.6を使用。

やりたいこと

  • CODE_171003/test_getdir_171003.py
  • CODE_171003/test_hello_171003_exec

CODE_171003より一つ上のディレクトリからtest_getdir_171003.pyを実行し、そのPython script内でbash script(test_hello_171003_exec)を実行したい。

参考

ディレクトリを探す方法は以下を参考にした。
https://stackoverflow.com/questions/4934806/how-can-i-find-scripts-directory-with-python

code

test_hello_171003_exec
#!/usr/bin/env bash
echo "zabuuun"

test_hello_171003_execにはchmod +xしておく。

test_getdir_171003.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import subprocess as sb

#on python2.6.6
#coding rule: PEP8


def invoke_script(ascript):
    #scd: script directory
    scd = os.path.dirname(os.path.realpath(__file__))
    acmd = scd + "/" + ascript
    sb.call(acmd.split())

invoke_script("test_hello_171003_exec")
run
$ python CODE_171003/test_getdir_171003.py 
zabuuun
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1