Parsing Command line arguments in python which has spaces

后端 未结 4 1217
借酒劲吻你
借酒劲吻你 2020-12-31 09:18

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

4条回答
  •  庸人自扰
    2020-12-31 09:50

    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.

提交回复
热议问题