-1

I have a Python method that first changes the working directory to a specific path and then needs to execute a command - below is an extract of same:

def start_testing_binary(self, command, path, binary, args, config):
    os.chdir(path)
    cmd = command.replace('"', '') + binary+ args.replace('"', '')  + config.replace('"', '') 
    return cmd

When I invoke the method with a system path having spaces in between I get below error:

start_testing_binary("myexe", "C:\\Users\\Project Work\\bin", ".exe", "-c" , "myinfo.ini")
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '"C:\\Users\\Project Work\\bin"'  

But when I hardcode its value in below call as mentioned:

os.chdir("C:\\Users\\Project Work\\bin")

The code does not reports any error - how can I fix this issue of having path being passed as argument?

Programmer
  • 8,303
  • 23
  • 78
  • 162
  • 1
    The first argument is `command`, not `path` - your example probably needs some correction, as the call you write there would generate an exception due to mismatched signatures. – holdenweb Apr 11 '19 at 15:32
  • Thanks corrected it - that was a typo issue – Programmer Apr 11 '19 at 15:35

1 Answers1

0

os.chdir seems to be very nice to accept the space.
Add \\ in front of it to escape it when used as an argument!?
Or put the path in quotes with \", or easier put the path as '"path next_part"' (single then double quotes) ?
See also How do I execute a program from Python? os.system fails due to spaces in path

B. Go
  • 1,436
  • 4
  • 15
  • 22