0

I am a novice user in python and I am having a problem in executing external command with command-line switch (using python 2.7 in Windows 8 64-bit):

lammpsExe = 'C:/Program Files (x86)/LAMMPS 32-bit 20150403/bin/lmp_serial.exe'
os.system(lammpsExe + " -in in.lmps")

It gives the following error message:

'C Program' is not recognized as an internal or external command, operable program or batch file

It seems that os.system cannot understand the string path for lammpsExe. Then I tried subprocess.call and replace '/' with '\\' in the path:

lammpsExe = 'C:\\Program Files\\LAMMPS 64-bit 20150330\\bin\\lmp_serial.exe'
subprocess.call([lammpsExe,'-in in.lmps'], shell=True)

But it still doesn't work as the command prompt gives the following warning:

IndentationError: unexpected indent

I suspect the command-line switch '-in' is the problem. I have tried various combination of ", ', \, and /, and I still get error messages.

Ocean Flyer
  • 51
  • 1
  • 3
  • You need to put double quotes around paths that contain embedded spaces: i.e. `'"C:/Program Files (x86)/LAMMPS 32-bit 20150403/bin/lmp_serial.exe"'` – martineau Apr 08 '15 at 16:27
  • @martineau It still doesn't work, even with the following variation: or or – Ocean Flyer Apr 08 '15 at 16:37
  • Even if it does not solve your entire problem, but it's definitely something you need to do. Try to come up with something that works from the command-prompt, then translate that into a `os.system()` call. – martineau Apr 08 '15 at 16:47

2 Answers2

0

The subprocess docs has a section titled Replacing os.system():

status = os.system("mycmd" + " myarg")
# becomes
status = subprocess.call("mycmd" + " myarg", shell=True)

I would suggest:

lammpsExe = 'C:\\Program Files\\LAMMPS 64-bit 20150330\\bin\\lmp_serial.exe'
subprocess.call(lammpsExe + ' -in in.lmps', shell=True)

The docs do note that "Calling the program through the shell is usually not required."

shudoh
  • 93
  • 1
  • 4
  • I have tried and still does not work. I am still trying to test various string commands via the command-prompt, but still no success yet – Ocean Flyer Apr 08 '15 at 17:20
  • Just a thought @ocean-flyer, does running `"C:\Program Files\LAMMPS 64-bit 20150330\bin\lmp_serial.exe" -in in.lmps` from the command line do what you expect it to do? – shudoh Apr 08 '15 at 17:28
  • I am trying to execute a simulation program (Lammps) based on an input file (in.lmps). I can do this from a command prompt easily, but I need to do this for 125 different input files (with the same file name but in different folders) and python is the best way to do this. – Ocean Flyer Apr 08 '15 at 17:43
0

I think I have found the solution. Instead of trying to define the path in the python script for the external command, I just need to put the path in the Windows system variables, then calling the command directly in the script:

subprocess.call(['lmp_serial.exe','-in','in.lmps'],shell=True)

Ocean Flyer
  • 51
  • 1
  • 3