12

I am invoking the script from ant . I am getting it as a single string from the caller but python is strangely treating it as two individual strings.I have script that reads a file name with it's path in windows. The folder structure may or may not have spaces in between

Here is an example

test.py D:/test/File Name

I know this can be done using optparse. Is there any way that i can read the param as single argument like i want to get it in sys.argv[index] (as a single string). I have tired prefixing with ' and " but with no success.

Raj
  • 1,250
  • 2
  • 11
  • 20
  • Are you invoking the Python script from another program? If so, there are interfaces to invoke a program with arbitrary strings as arguments, without having to worry about things like spaces or quotation marks. – user2357112 Dec 26 '13 at 13:09
  • Just quote the arguments. Filenames should not be meant to contain spaces, just due to things like this. But since Windows made it standard anyway, you leave that to the script caller: use quotes. Above all, it will have a consistent approach to any other command line tool your users will have to use, in that, filenames containing space must be quoted. Having your program behaving differently will just add confusion. – jsbueno Dec 26 '13 at 14:40
  • Can you please point me to the location where i can find such an interfaces – Raj Dec 26 '13 at 18:23

4 Answers4

15

You pass the folder name wrapped in quotes:

test.py "D:\test\File Name"

sys.argv[1] will contain the folder path, spaces included.

If for some reason you cannot quote the folder name, you will need to use the ctypes module and use the Win32 API's GetCommandLine function. Here's a functional example.

Community
  • 1
  • 1
Blender
  • 289,723
  • 53
  • 439
  • 496
  • I did try with a quote with no sucess the version of python i am using is too old i.e. 2.2 – Raj Dec 26 '13 at 13:03
  • @Raj: How are you running it? Python is passed the list of arguments, it doesn't parse them on its own. – Blender Dec 26 '13 at 13:09
  • 1
    python 2.2 is like..more than 10 years old. You really should get Python 2.7 or 3.3 there. – jsbueno Dec 26 '13 at 14:39
  • thing is like it comes with weblogic server and wlst uses that version i can't really change the version though – Raj Dec 26 '13 at 18:14
  • 1
    The real problem was with the ant task which i have changed to a java task it works fine thanks for the response – Raj Dec 27 '13 at 08:38
  • This doesn't work on linux. How can it be done there with the optparse or argparse module? – MathKid Oct 24 '16 at 21:00
  • @CodeKid: it should. – Blender Oct 24 '16 at 21:01
  • After a closer look, it seems it does work if the python script is called directly on the command line. However, if the arguments are passed to a bash function, which in turn calls python to run a script and passes on the arguments then it does not. – MathKid Oct 24 '16 at 21:09
1

According to MS: https://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx

Backslashes are interpreted literally, unless they immediately precede a double quotation mark.

I'm wondwring if Python on Windows uses this method: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx

To parse the command line to create sys.argv. In theory it should do it.

rressi
  • 21
  • 2
0

The convention for passing spaces as arguments is by escaping spaces.

test.py D:/test/File\ Name

This way you'll have access to "D:/test/File Name" in your python script.

Ruslan Osipov
  • 5,655
  • 4
  • 29
  • 44
-1

EDIT!! :
Depending on the encoder of your command line,
The ascii encoding reference for a space is normally %20
So instead of using a space use %20
https://www.w3schools.com/tags/ref_urlencode.ASP

As below

test.py "D:\test\File%20Name"

Be sure to write it as it is

Please vote in support if this helps you out. It wasn't as correct the first time I wrote it but now I believe that should do.

Don
  • 1
  • 1