0

I would like to call a script named openseessp and then pass "source test.tcl" argument to this script.

I tried with subprocess module but after it invoked openseessp it exits and then runs source test.tcl command. I need to run this without exiting the first (openseessp):

subprocess.run(['openseessp', 'source test.tcl'], shell=True, cwd=directoryJob)

Update: for the record, the code above works. The problem was related to openseessp. I changed openseessp (single processor version) to openseesmp (multiprocessor version) and it worked.

Biblo
  • 15
  • 3
  • what file is openseessp? you passed two entries openseessp and source test.tcl to subprocess.run which roughly translates to running "openseessp source test.tcl" which doesn't seem right. is this your intention? – Sin Han Jinn Dec 28 '22 at 03:31
  • I need to call openseessp first, source test.tcl should be run inside openseessp. – Biblo Dec 28 '22 at 03:39
  • yeah i think @Selcuk answer below solves your problem – Sin Han Jinn Dec 28 '22 at 03:40

1 Answers1

0

From the docs:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

That is, either pass a string with shell=True,

subprocess.run('openseessp source test.tcl', shell=True, cwd=directoryJob)

or pass a list (shell is False by default).

subprocess.run(['openseessp', 'source test.tcl'], cwd=directoryJob)
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • Thank you but its not working this way. openseessp and source test.tcl are not separate commands. openseessp is another program and source test.tcl need to be passed to it. What I would like to do is: start openseessp first and then call "source test.tcl" command within openseessp. – Biblo Dec 28 '22 at 03:42
  • What do you mean by "passed to it"? This is how you pass arguments to commands. How do you manually run it? – Selcuk Dec 28 '22 at 04:15
  • Manually I open cmd.exe and then type openseessp enter. it loads up opensees software. After this, I enter command source test.tcl and enter. thats it. When I try your code above, it loads openseessp yes but it exits the program and then executes second command. Since the second command executed outside of openseessp it cannot run test.tcl properly. – Biblo Dec 28 '22 at 04:25
  • I see. That's not "passing an argument". You seem to be running an interactive command line tool. See [this](https://stackoverflow.com/questions/8475290/how-do-i-write-to-a-python-subprocess-stdin) for an example of how to do that in Python. – Selcuk Dec 28 '22 at 04:29