Nov-30-2017, 01:04 PM
Hi there,
I'm having issues with both subprocess and call functions to accept spaces in the the file path.
I'm running on windows and both of the below commands work manually in the Windows shell:
cmd /c C:\!onetwo\me.bat #no space
cmd /c "C:\!one two\me.bat" #with space
Both Call and Subprocess return the same error and it's to do with the space in the path. I'll use Call for this example. I'm leaving the double quotes around the path even without spaces just for simplicity.
1. Works(no space in path):
strFilePath = "C:\\!onetwo\\me.bat"
print("out: " + strFilePath)
call(["cmd.exe", "/c " + strFilePath])
2. Fails (space in path - path not encased within double quotes): -- I'd expect the below error.
strFilePath = "C:\\!one two\\me.bat"
print("out: " + strFilePath)
call(["cmd.exe", "/c " + strFilePath])
3. Fails (space in path - path IS encased within double quotes): -- I'd expect this to work!!
strFilePath = "C:\\!one two\\me.bat"
strFilePath = "\"" + strFilePath + "\""
print("out: " + strFilePath)
call(["cmd.exe", "/c " + strFilePath])
Any ideas how I can get around this? Unfortunately, I'm calling a 3rd party application that will always have spaces in the install path so I can't change the install location.
Many thanks,
John
I'm having issues with both subprocess and call functions to accept spaces in the the file path.
I'm running on windows and both of the below commands work manually in the Windows shell:
cmd /c C:\!onetwo\me.bat #no space
cmd /c "C:\!one two\me.bat" #with space
Both Call and Subprocess return the same error and it's to do with the space in the path. I'll use Call for this example. I'm leaving the double quotes around the path even without spaces just for simplicity.
1. Works(no space in path):
strFilePath = "C:\\!onetwo\\me.bat"
print("out: " + strFilePath)
call(["cmd.exe", "/c " + strFilePath])
Quote:out: C:\!onetwo\me.bat
2. Fails (space in path - path not encased within double quotes): -- I'd expect the below error.
strFilePath = "C:\\!one two\\me.bat"
print("out: " + strFilePath)
call(["cmd.exe", "/c " + strFilePath])
Quote:out: C:\!one two\me.bat
'C:\!one' is not recognized as an internal or external command,
operable program or batch file.
3. Fails (space in path - path IS encased within double quotes): -- I'd expect this to work!!
strFilePath = "C:\\!one two\\me.bat"
strFilePath = "\"" + strFilePath + "\""
print("out: " + strFilePath)
call(["cmd.exe", "/c " + strFilePath])
Quote:out: "C:\!one two\me.bat"
'\"C:\!one two\me.bat\""' is not recognized as an internal or external command,
operable program or batch file.
Any ideas how I can get around this? Unfortunately, I'm calling a 3rd party application that will always have spaces in the install path so I can't change the install location.
Many thanks,
John