1

I have a python program that is calling an external command. The command needs to look like this:

java -jar ../GeoNetCWBQuery-4.0.2-bin.jar -s "NZMQZ..HH..." -d 3600 -event:time
"2004/12/26 00:58:50" -event:lat "3.3" -event:lon "95.78" -event:depth "10.0"
-o %c%s%y%/M%/D%z

In the python program I have assigned names to the values which need to go into the command i.e. date, time, lat, lon, depth. (I can't just type in the values as I am looping over a huge file.) So my question is, how do I write it out i.e how do I insert the values into the command correctly. At the moment I have (which is not working):

os.system('java -jar GeoNetCWBQuery-4.0.2-bin.jar -s "NZMQZ..HH..." -d 3600
-event:time " + date + +time + " -event:lat " + lat + " -event:lon " + lon +
" -event:depth " + depth + " -o %c%s%y%M%D%z')
Udo Klein
  • 6,381
  • 1
  • 31
  • 53
Kg123
  • 119
  • 1
  • 12

1 Answers1

2

You have some trouble with single/double quotes.

os.system('java -jar GeoNetCWBQuery-4.0.2-bin.jar -s "NZMQZ..HH..." -d 3600
-event:time ' + date + time + ' -event:lat ' + lat + ' -event:lon ' + lon +
' -event:depth ' + depth + ' -o %c%s%y%M%D%z')

Also, as Udo Klein notes you should not use os.system anymore, prefer subprocess.call. If you are going to change os.system to subprocess.call you code will look like:

subprocess.call('java', ['-jar', 'GeoNetCWBQuery-4.0.2-bin.jar', '-s', 'NZMQZ..HH...', '-d', '3600',
'-event:time', date, time,  '-event:lat', lat, '-event:lon', lon, 
'-event:depth', depth, '-o', '%c%s%y%M%D%z'])
sasha.sochka
  • 12,899
  • 8
  • 41
  • 64
  • @sasha.sochka: How can I call `C:\Program Files (x86)\MSBuild\12.0\Bin\msbuild "D:\WebService\IDEWebService.sln" /p:DeployOnBuild=true /p:PublishProfile="D:\WebService\Properties\PublishProfiles\MyDeployment.pubxml" /p:AllowUntrustedCertificate=true /p:UserName=Admin /p:Password=password` from python3.4 ` – Nevin Raj Victor Jun 04 '15 at 05:32