Calling a python script with input within a python script using subprocess

前端 未结 2 764
暖寄归人
暖寄归人 2020-11-22 15:32

I have a script a.py and while executing it will ask certain queries to user and frame the output in json format. Using python subprocess, I am able to call thi

2条回答
  •  [愿得一人]
    2020-11-22 15:58

    Another way than mentioned, is by using the built-in funtion exec
    This function gets a string of python code and executes it
    To use it on a script file, you can simply read it as a text file, as such:

    #dir is the directory of a.py
    #a.py, for example, contains the variable 'x=1'
    exec(open(dir+'\\a.py').read())
    print(x) #outputs 1
    

提交回复
热议问题