0

I have a python program which needs to be executed on both windows and linux. In windows alone it is automatically appending single quotes '' to the arguments. I'm using argparser to pass the arguments. Below is my code: test.py:

def test_func():
   pass1 =args.testpassword
   user =args.testuser  
   print(pass1)
   print(user)
   print('{}@{}'.format(user, pass1))

def main():
   parser = argparse.ArgumentParser()
   parser.add_argument("--user", required=True, dest='testuser')
   parser.add_argument("--password", required=True, dest='testpassword')
   args = parser.parse_args()
   test_func(args)

test.py --user 'testuser' --password 'testpass'

Output is: 'testuser'

'testpass'

'testuser'@'testpass'

the same script when executed in linux doesn't append the quotes and the output is:

testuser

testpass

testuser@testpass

In both cases i'm running on python-2.7.11 but not sure why in windows alone i'm getting these quotes. This is causing problem as my other module where i pass these values are expecting it without quotes. I even tried encoding the string using UTF-8 and it still didn't help. What am I doing incorrectly?

Sandy
  • 187
  • 3
  • 16
  • 1
    `pass` is a keyword. Anyway, the problem is that the VC++ startup code in Windows Python doesn't use single quotes for arguments when creating the `argv` array from the command line. It only uses double quotes. – Eryk Sun May 13 '17 at 17:08
  • Here are the [rules for command-line arguments](https://msdn.microsoft.com/en-us/library/17w5ykft.aspx) that Windows Python uses -- at least the official distribution that's built using Visual Studio. – Eryk Sun May 13 '17 at 17:12
  • @eryksun Fantastic! That worked! Thanks a lot! – Sandy May 13 '17 at 17:26
  • Possible duplicate of [Single quotes and double quotes: How to have the same behaviour in unix and windows?](http://stackoverflow.com/questions/10737283/single-quotes-and-double-quotes-how-to-have-the-same-behaviour-in-unix-and-wind) – gz. May 13 '17 at 17:57
  • The answer to [What does single quote do in windows batch files?](http://stackoverflow.com/questions/24173825/what-does-single-quote-do-in-windows-batch-files) will tell you what you need to know about windows command line quoting. – gz. May 13 '17 at 17:59
  • @gz, that's the quoting rules used for cmd.exe, which may be important to this problem if the purpose of the quotes here is to also prevent cmd.exe from parsing special characters such as `>` and `&`. But it's irrelevant to how arguments are parsed in the child process. It's not like a Unix shell that splits up the command line into arguments to pass to `execl` or `execv`. Windows programs just see a command-line string. – Eryk Sun May 13 '17 at 18:13

0 Answers0