Friday, September 8, 2017

ouput the standard output to variable

1. 引入library
import subprocess

2. 定義函數
def get_cmd_line_results(input_string):
    os.chdir(INET_DIR)
    proc = subprocess.Popen(input_string, stdout=subprocess.PIPE, shell=True)
    #"git symbolic-ref HEAD"
    (out, err) = proc.communicate()
    os.chdir(CUR_DIR)
    return out[:-1]



3. 在主程式內容呼叫
inet_branch = get_cmd_line_results("git symbolic-ref HEAD")

No comments:

Post a Comment