Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25742 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2012-07-21 14:20 +0200 |
| Last post | 2012-07-21 09:03 -0700 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Calling Java jar class with parameter from Python Peter Otten <__peter__@web.de> - 2012-07-21 14:20 +0200
Re: Calling Java jar class with parameter from Python Roy Smith <roy@panix.com> - 2012-07-21 09:57 -0400
Re: Calling Java jar class with parameter from Python jasonveldicott@gmail.com - 2012-07-21 09:11 -0700
Re: Calling Java jar class with parameter from Python jasonveldicott@gmail.com - 2012-07-21 09:03 -0700
Re: Calling Java jar class with parameter from Python jasonveldicott@gmail.com - 2012-07-21 09:03 -0700
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-07-21 14:20 +0200 |
| Subject | Re: Calling Java jar class with parameter from Python |
| Message-ID | <mailman.2380.1342873263.4697.python-list@python.org> |
Jason Veldicott wrote: > subprocess.Popen(["C:\\Program Files > (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", "-cp > c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool", > "C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"], > stdout=subprocess.PIPE, shell=True ).communicate() > > > Obviously, some trick is being missed. Could anyone shed light on what it > may be? File names with spaces can be tricky. Try thoroughly separating the individual arguments and let subprocess do the necessary escaping. I think it should be subprocess.Popen([ "C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", "-cp", "C:\\antlr\\antlr-3.4-complete.jar", "org.antlr.Tool", "C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"], stdout=subprocess.PIPE).communicate()
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-07-21 09:57 -0400 |
| Message-ID | <roy-B96A3E.09574821072012@news.panix.com> |
| In reply to | #25742 |
In article <mailman.2380.1342873263.4697.python-list@python.org>,
Peter Otten <__peter__@web.de> wrote:
> subprocess.Popen([
> "C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe",
> "-cp",
> "C:\\antlr\\antlr-3.4-complete.jar",
> "org.antlr.Tool",
> "C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"],
> stdout=subprocess.PIPE).communicate()
You might also want to try raw strings. This should be identical to
Peter's version, but easier to read:
subprocess.Popen([
r"C:\Program Files (x86)\Java\jdk1.7.0_05\bin\java.exe",
r"-cp",
r"C:\antlr\antlr-3.4-complete.jar",
r"org.antlr.Tool",
r"C:\Users\Jason\Documents\antlr\java grammar\Java.g"],
stdout=subprocess.PIPE).communicate()
although I would probably refactor it like:
args = [r"C:\Program Files (x86)\Java\jdk1.7.0_05\bin\java.exe",
r"-cp",
r"C:\antlr\antlr-3.4-complete.jar",
r"org.antlr.Tool",
r"C:\Users\Jason\Documents\antlr\java grammar\Java.g",
]
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
proc.communicate()
[toc] | [prev] | [next] | [standalone]
| From | jasonveldicott@gmail.com |
|---|---|
| Date | 2012-07-21 09:11 -0700 |
| Message-ID | <1d773886-2cd0-4f72-b061-1490db6567e8@googlegroups.com> |
| In reply to | #25743 |
On Saturday, July 21, 2012 6:57:48 AM UTC-7, Roy Smith wrote: > In article <mailman.2380.1342873263.4697.python-list@python.org>, > Peter Otten <__peter__@web.de> wrote: > > > subprocess.Popen([ > > "C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", > > "-cp", > > "C:\\antlr\\antlr-3.4-complete.jar", > > "org.antlr.Tool", > > "C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"], > > stdout=subprocess.PIPE).communicate() > > You might also want to try raw strings. This should be identical to > Peter's version, but easier to read: > > subprocess.Popen([ > r"C:\Program Files (x86)\Java\jdk1.7.0_05\bin\java.exe", > r"-cp", > r"C:\antlr\antlr-3.4-complete.jar", > r"org.antlr.Tool", > r"C:\Users\Jason\Documents\antlr\java grammar\Java.g"], > stdout=subprocess.PIPE).communicate() > > although I would probably refactor it like: > > args = [r"C:\Program Files (x86)\Java\jdk1.7.0_05\bin\java.exe", > r"-cp", > r"C:\antlr\antlr-3.4-complete.jar", > r"org.antlr.Tool", > r"C:\Users\Jason\Documents\antlr\java grammar\Java.g", > ] > proc = subprocess.Popen(args, stdout=subprocess.PIPE) > proc.communicate() The r string notation at least saves having to double type a bunch of backslashes, although the appearance prepended to the string takes a little getting used to. Visually the separate array to handle arguments is perhaps cleaner, having more resemblance to the original command. Thanks for the tips.
[toc] | [prev] | [next] | [standalone]
| From | jasonveldicott@gmail.com |
|---|---|
| Date | 2012-07-21 09:03 -0700 |
| Message-ID | <mailman.2386.1342886640.4697.python-list@python.org> |
| In reply to | #25742 |
On Saturday, July 21, 2012 5:20:48 AM UTC-7, Peter Otten wrote: > Jason Veldicott wrote: > > > subprocess.Popen(["C:\\Program Files > > (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", "-cp > > c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool", > > "C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"], > > stdout=subprocess.PIPE, shell=True ).communicate() > > > > > > Obviously, some trick is being missed. Could anyone shed light on what it > > may be? > > File names with spaces can be tricky. Try thoroughly separating the > individual arguments and let subprocess do the necessary escaping. > I think it should be > > subprocess.Popen([ > "C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", > "-cp", > "C:\\antlr\\antlr-3.4-complete.jar", > "org.antlr.Tool", > "C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"], > stdout=subprocess.PIPE).communicate() That did the trick, thanks. I had the impression from another post that the breaking up of command strings into subprocess arguments could be done arbitrarily as needed to deal with nested inverted commas. Obviously as you've shown, this is not the case, at least for Popen.
[toc] | [prev] | [next] | [standalone]
| From | jasonveldicott@gmail.com |
|---|---|
| Date | 2012-07-21 09:03 -0700 |
| Message-ID | <f8b2335d-3beb-4b58-9fe1-9432a08dd457@googlegroups.com> |
| In reply to | #25742 |
On Saturday, July 21, 2012 5:20:48 AM UTC-7, Peter Otten wrote: > Jason Veldicott wrote: > > > subprocess.Popen(["C:\\Program Files > > (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", "-cp > > c:\\antlr\\antlr-3.4-complete.jar org.antlr.Tool", > > "C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"], > > stdout=subprocess.PIPE, shell=True ).communicate() > > > > > > Obviously, some trick is being missed. Could anyone shed light on what it > > may be? > > File names with spaces can be tricky. Try thoroughly separating the > individual arguments and let subprocess do the necessary escaping. > I think it should be > > subprocess.Popen([ > "C:\\Program Files (x86)\\Java\\jdk1.7.0_05\\bin\\java.exe", > "-cp", > "C:\\antlr\\antlr-3.4-complete.jar", > "org.antlr.Tool", > "C:\\Users\\Jason\\Documents\\antlr\\java grammar\\Java.g"], > stdout=subprocess.PIPE).communicate() That did the trick, thanks. I had the impression from another post that the breaking up of command strings into subprocess arguments could be done arbitrarily as needed to deal with nested inverted commas. Obviously as you've shown, this is not the case, at least for Popen.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web