0

I am using subprocess.call() to execute commands from a specific directory.

Without using shell = True

scrcpyPath = "C:\\Users\\H\\Downloads\\scrcpy-win64-v1.14"
subprocess.call(["scrcpy", "--window-title", "'Mydevice'"], cwd = scrcpyPath)

I get the following error

FileNotFoundError: [WinError 2] The system cannot find the file specified.

I have managed to make it work using `shell = True'

subprocess.call(f"scrcpy --window-title 'Mydevice'", cwd = scrcpyPath, shell = True)

but it stops working when I add whitespace in the window title Mydevice.

subprocess.call(f"scrcpy --window-title 'My device'", cwd = scrcpyPath, shell = True)

I get the following error

ERROR: Unexpected additional argument: 1'

The reason I am using a formatted string is that I want the window title to be a variable, but again - it does not work when I add whitespace.

subprocess.call(f"scrcpy --window-title 'Device {deviceName}'", cwd = scrcpyPath, shell = True)

I found the solution. I think I was wrongly using cwd.

subprocess.call([f"{scrcpyPath}\\scrcpy", "--window-title", f"'{deviceName}'"])
2
  • 2
    With shell = True, python uses the shell to find where the binary file for the command scrcpy is located. If you don't use that, you need to tell python about the location of that binary yourself. Commented Jul 2, 2020 at 11:45
  • @rdas I am not sure what that means, but I will look it up. I thought the location is given by cwd. Commented Jul 2, 2020 at 11:57

1 Answer 1

3

You didn't split your arguments properly; basically any space-separation in the command-line should be separate arguments, and no shell quoting is needed when they're passed as separate arguments. What you wanted was:

subprocess.call(["scrcpy", "--window-title", "Mydevice"], cwd=scrcpyPath)
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried that and I have tried subprocess.call(["scrcpy"], cwd = scrcpyPath). It still does not work without shell = True. I get this error: FileNotFoundError: [WinError 2] The system cannot find the file specified

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.