-1

I would like to run a python script from enthought canopy v1.5.0.2717, either in mac or windows, and provide a absolute file path as an argument using the run configuration dialog.

In the run configuration I put an argument (for example):

'/Users/dir/Data/University stuff/CQT/Data/ScriptRunFolder/testingPythonRfRamp.xml' 

The my script contains the following code:

import sys
print sys.argv[1]

I then click "run" and the printed string is:

'/Users/dir/Data/University'

Another example is using the path:

'C:\User\Program files\test.txt'

and it prints

'C:UserProgram'

It looks like it splits the path at the spaces, and deletes the "\".

Running the script from the command line like:

$python myScript.py '/Users/dir/Data/University stuff/CQT/Data/ScriptRunFolder/testingPythonRfRamp.xml'

Results in the correct printed string:

'/Users/dir/Data/University stuff/CQT/Data/ScriptRunFolder/testingPythonRfRamp.xml'

How can I achieve the same result using Canopy?

labjunky
  • 831
  • 1
  • 13
  • 22
  • use os.path.sep to identify current file seperator in running OS – Shawn Zhang Nov 26 '14 at 04:43
  • What have you tried, what error message are you seeing, why don't you post some code? – Amit Kumar Gupta Nov 26 '14 at 04:47
  • @ShawnZhang I was more thinking about the spaces. I know and I assume the user knows what file separator to use. Plus "\" is a special character in a python string, should I input it as "\\" in the path? And how do I represent the spaces in the path also? – labjunky Nov 26 '14 at 04:48
  • @AmitKumarGupta I used an argument '/Users/dir/Data/University stuff/CQT/Data/ScriptRunFolder/testingPythonRfRamp.xml' and called print sys.argv[1] which results in '/Users/dir/Data/University' – labjunky Nov 26 '14 at 04:51
  • 1
    Could you edit your post to include the commands you are running as well as your code? – Alex Nov 26 '14 at 05:09
  • looks like its seperating args based on spaces. Why not combine the elements of the arrays with spaces joining them? It's a little sketchy but I don't see why it wouldn't work – ghostbust555 Nov 26 '14 at 05:10
  • @ghostbust555 thanks, I think this would work. However, it may confuse things when the script gets more than one argument and the path has no spaces. Maybe there is a more generic procedure to sort this out? – labjunky Nov 26 '14 at 05:14
  • 1
    Do you mean you ran `python myScript.py '/Users/dir/Data/University stuff/CQT/Data/ScriptRunFolder/testingPythonRfRamp.xml'` when you say you "passed an argument"? Also, could you paste the relevant sections of code (which is maybe +/- 5 lines around the actual lines to provide context) that touch the system arguments? – Alex Nov 26 '14 at 05:31
  • I'm on a Mac, using Python 2.7.5. I do `python str.py '/Users/foo/bar baz qux/foo.xml'` on the command line, and the contents of `str.py` are `import sys; print sys.argv[1]`. The output is `/Users/foo/bar baz qux/foo.xml`. So it works on my machine. Please post *exactly* the contents of your script (or a pared down version that exemplifies the problem), *exactly* what you run on the command line, and *exactly* what output you see. – Amit Kumar Gupta Nov 26 '14 at 05:36
  • Use double quotes instead of single quotes. – tdelaney Nov 26 '14 at 05:47
  • @AmitKumarGupta You are right. The problem is in fact with Enthought canopy's python. There you can specify the arguments in the run configuration. But if I run the same script from the terminal I get the correct response. So its a canopy issue and not a python one. Thanks for your comments they've been helpful to track it down. – labjunky Nov 26 '14 at 05:55
  • @labjunky I see very clearly that if I edit a run configuration "Run-> Run Configurations -> Run Configurations" and use the "Arguments" control of that dialog box, Canopy strips my quotes out of my arguments before sending them to the %run magic in ipython changing `print sys.argv[1]` to `sys.argv[1:]` and watching the ipython echo is illustrative. In this case, an entry in the run dialog box reading `foo, 'bar', 'c:\foo bar\baz'` is written as input to ipython as `in[nn] ... foo, bar, c:\foo bar\baz` As you can see, the input is changed. I'd tend to think of this as a bug, have reported it ? – OYRM Dec 01 '14 at 17:02

1 Answers1

0

The shell will group everything within double-quotes into a single parameter, not single quotes.

>type showme.py
import sys
print(sys.argv[1:])


>python showme.py 'i am sad'
["'i", 'am', "sad'"]
>python showme.py "i am happy"
['i am happy']
tdelaney
  • 73,364
  • 6
  • 83
  • 116