Dinesh Pundkar Dinesh Pundkar - 2 months ago 8
Python Question

Issue with arguments with quotes in Robot Framework

I am trying to run a simple python script which accept one argument and print it.
I am executing this python script using keyword Run present in Operating System library of Robot Framework.

I am trying to execute in following two ways:

${rc} ${output}= Run And Return Rc And Output python "C:\\Users\\Administrator\\Desktop\\abc.py" "C:\Program Files (x86)\Common Files"


Output is coming proper in this case :

Documentation:
Runs the given command in the system and returns the RC and output.
Start / End / Elapsed: 20151119 16:01:57.147 / 20151119 16:01:57.179 / 00:00:00.032
16:01:57.147 INFO Running command 'python "C:\Users\Administrator\Desktop\abc.py" "C:Program Files (x86)Common Files" 2>&1'.
16:01:57.179 INFO ${rc} = 0
16:01:57.179 INFO ${output} = C:Program Files (x86)Common Files


However when I try to run in below way, it's giving error :

${rc} ${output}= Run And Return Rc And Output "C:\\Python27\\python.exe" "C:\\Users\\Administrator\\Desktop\\abc.py" "C:\Program Files (x86)\Common Files"


Error:

16:12:44.481 INFO Running command '"C:\Python27\python.exe" "C:\Users\Administrator\Desktop\abc.py" "C:Program Files (x86)Common Files" 2>&1'.
16:12:44.481 INFO ${rc} = 1
16:12:44.481 INFO ${output} = The filename, directory name, or volume label syntax is incorrect.


If I run same thing on command prompt, it works fine.

C:\Windows>"C:\Python27\python.exe" "C:\Users\Administrator\Desktop\abc.py" "C:Program Files (x86)Common Files"
C:Program Files (x86)Common Files


Just want to know where I am going wrong for second case with Robot Framework?

Answer

RobotFramework is not using a shell to run the command, but rather is passing it verbose to Windows. So it's looking for a filed named literally "C:\Python27\python.exe" instead of C:\Python27\python.exe. Note the wrapping quotes, usually those quotes are processed by the shell before invoking the command. So, you should change the command to C:\\Python27\\python.exe "C:\\Users\\Administrator\\Desktop\\abc.py" "C:\Program Files (x86)\Common Files"

To better understand the difference of the shell argument you can try to compare the result of

subprocess.check_call('"C:\Python27\python.exe" "C:\Users\Administrator\Desktop\abc.py" "C:Program Files (x86)Common Files"')

versus

subprocess.check_call('"C:\Python27\python.exe" "C:\Users\Administrator\Desktop\abc.py" "C:Program Files (x86)Common Files"', shell=True)
Comments