Python Forum
Can't get call or subprocess functions to use space in path.
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't get call or subprocess functions to use space in path.
#1
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])
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
Reply
#2
"/c " + strFilePat is not +.
import subprocess

subprocess.call(['cmd', '/c', 'E:/1py_div/ttk/dist/tk_test.exe'])
/c tells cmd to run tk_test.exe placed in this Path.

Doc
Quote:The recommended approach to invoking subprocesses is to use the run()
So for 3.5 or higher should use run().
import subprocess

subprocess.run(['cmd', '/c', 'E:/1py_div/ttk/dist/tk_test.exe'])
Reply
#3
Hi guys,

Legends!  Thanks so much.

Looks as if in my subprocess call, I did not use the '-c' as a separate argument.  School boy error I suppose :)

Thanks so much, really appreciate your quick response!!

Cheers,
J
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,149 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  continue if 'subprocess.call' failes tester_V 11 5,009 Aug-26-2021, 12:16 AM
Last Post: tester_V
  printing out the contents aftre subprocess.call() Rakshan 3 2,701 Jul-30-2021, 08:27 AM
Last Post: DeaD_EyE
  Subprocess.Popen() not working when reading file path from csv file herwin 13 14,619 May-07-2021, 03:26 PM
Last Post: herwin
  How to call multiple functions sequentially Mayo 2 9,159 Jan-06-2021, 07:37 PM
Last Post: Mayo
  subprocess call cannot find the file specified RRR 6 16,384 Oct-15-2020, 11:29 AM
Last Post: RRR
  from global space to local space Skaperen 4 2,269 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  module to store functions/variables and how to call them? mstichler 3 2,339 Jun-03-2020, 06:49 PM
Last Post: mstichler
  Why wont subprocess call work? steve_shambles 3 2,604 Apr-28-2020, 03:06 PM
Last Post: steve_shambles
  subprocess.call - Help ! sniper6 0 1,498 Nov-27-2019, 07:42 PM
Last Post: sniper6

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020