Reputation: 117
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')
Upvotes: 1
Views: 264
Reputation: 14715
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'])
Upvotes: 2