-1

I have a command like that spins off some calculations and stores some output in a folder. So, I have this command that works on the bash command (in Ubuntu).

/home/usr2/AXE/prog1.exe -k 1 -m /home/usr2/AXE/config.txt

However, I would like to "call" this from inside a python program.

The final idea is to generate lots of serial processes of this nature inside python.

/home/usr2/AXE/prog1.exe -k 1 -m /home/usr2/AXE/config.txt
/home/usr2/AXE/prog1.exe -k 2 -m /home/usr2/AXE/config.txt
/home/usr2/AXE/prog1.exe -k 3 -m /home/usr2/AXE/config.txt
...
...
/home/usr2/AXE/prog1.exe -k 2000 -m /home/usr2/AXE/config.txt

SO FAR

import os
os.system('"/home/usr2/AXE/prog1.exe -k 1 -m /home/usr2/AXE/config.txt"')

This gives me command line output of Out[11]: 32512, but the output file is not produced in the requisite folder.

What am I doing wrong?

P.S. No idea about Bash programming. But would be interested in Bash solutions as well.

maximusdooku
  • 5,242
  • 10
  • 54
  • 94
  • 1
    `os.system()` is old, hard-to-use-correctly, and is replaced by [the `subprocess` module](https://docs.python.org/2/library/subprocess.html). Also, you're using too many quotes -- don't put literal quotes inside your syntactic quotes unless doing so results in a command you could *actually* pass to a shell with those literal quotes included. – Charles Duffy Aug 01 '17 at 23:01
  • 1
    that said, the bash answer is shorter here: `for ((i=1; i<=2000; i++)); do /home/usr2/AXE/prog1.exe -k "$i" -m /home/usr2/AXE/config.txt; done` – Charles Duffy Aug 01 '17 at 23:02
  • Thanks! I was following this answer. https://stackoverflow.com/questions/13222808/how-to-run-external-executable-using-python I'll try it – maximusdooku Aug 01 '17 at 23:04
  • The inner quotes on that answer are around **the executable**, not the whole command. (It's just that in that example, the executable *is* the whole command). It's the right thing if you're passing an argument with spaces, for example, but not the right thing if you want the spaces to separate a command from its arguments or two arguments from each other. – Charles Duffy Aug 01 '17 at 23:06
  • @maximusdooku Please delete your question if it is answered somewhere else. The problem, as you reported it, is just a syntax (quoting) error. Besides that, I recommend to use the bash script CharlesDuffy gave you. – hek2mgl Aug 01 '17 at 23:07

1 Answers1

0

The immediate problem seems to be an excess of quotation marks. os.system takes a string containing the command you want to execute. So to execute the shell command echo hello, you would call os.system('echo hello') from Python. Calling os.system('"echo hello"') tries to execute the shell command "echo hello"; in other words, it looks for a single executable file in your PATH named echo hello, which presumably doesn't exist and certainly isn't what you want. So get rid of one set of quotes. Either one is fine since Python treats single and double quotes the same.

You can also try replacing your use of os.system with subprocess.run, also from the standard library. Instead of a single string containing the command, you pass it a sequence of strings, each of which is one token from the shell command you'd like to run. So in our example you could do subprocess.run(('echo', 'hello')). The os.system function is more or less deprecated in favor of subprocess.run.

Sam Marinelli
  • 999
  • 1
  • 6
  • 16