0

i tried both this:

import os

os.system(r"cd C:\daten\super pooper\use\app\nircmd && nircmd setdefaultsounddevice 'BD990P' 1")

where i got no error but the audio device didnt change either and this:

import os

os.system(r"cd C:\daten\super pooper\use\app\nircmd")
os.system("nircmd setdefaultsounddevice 'BD990P' 1")

where i got the error

"Der Befehl "nircmd" ist entweder falsch geschrieben oder konnte nicht gefunden werden."

which basically means that the command "nircmd" wasnt found that makes me think that every command is executed seperately and not taking into account direcotry changes that happened before, but even with my other try where that wouldnt have been an issue it doesnt work, so im kinda lost

Reschif
  • 11
  • 2
  • That is a really completely wrong approach. [os.system](https://docs.python.org/3/library/os.html#os.system) is deprecated since many years and should not be used anymore in new written Python scripts. There should be used the [subprocess module](https://docs.python.org/3/library/subprocess.html). The reasons are quite simple. `os.system` uses on Windows the Windows kernel library function [CreateProcess](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) to start `cmd.exe` with option `/c` and the specified command line appended. – Mofi Mar 05 '22 at 17:37
  • That makes the command line tricky to code as it can be read on running in a command prompt window `cmd /?` to get output the usage help of the Windows command processor, especially on using a command line containing operators like `&&` on which special syntax is required to get the command line with multiple commands executed successfully by `cmd.exe` using also `CreateProcess` to run executables like `nircmd.exe` on being found at all using the environment variables `PATHEXT` and `PATH` on being specified with just the file name without file extension and without full path. – Mofi Mar 05 '22 at 17:40
  • The `subprocess` module is on Windows a Python wrapper for the Windows kernel library function `CreateProcess` giving the Python script programmer full control over all parameters of `CreateProcess` and the [STARTUPINFO](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure. So in your case the usage of `subprocess.run` makes it possible to run `nircmd.exe` with its full path directly using `CreateProcess` without using `cmd.exe` at all making the task much easier to code. – Mofi Mar 05 '22 at 17:42
  • The current working directory for `nircmd.exe` can be specified directly in the Python script on using `subprocess` with the argument `cwd` which results in passing the current working directory string via function parameter `lpCurrentDirectory` (long pointer to current directory string) to `CreateProcess` which sets this directory as current working directory for the started executable. – Mofi Mar 05 '22 at 17:48
  • So just use `subprocess.run()` with the appropriate data for the parameters `args` and `cwd` to run `C:\daten\super pooper\use\app\nircmd\nircmd.exe` in the directory `C:\daten\super pooper\use\app\nircmd` with the arguments `setdefaultsounddevice`, `BD990P` and `1` (no `'` needed here at all) and your problem is solved just by using the right procedure with the right arguments. – Mofi Mar 05 '22 at 17:49

2 Answers2

0

The Path has to be set into a string. A space in a name would set it as a seperate parameter. So it should work if you do:

import os

os.system(r"cd C:\daten\'super pooper'\use\app\nircmd")
os.system("nircmd setdefaultsounddevice 'BD990P' 1")
MCLP2005
  • 1
  • 1
  • That is complete nonsense and does not work. Each call of `os.system()` results in starting on Windows `cmd.exe` with option `/c` and the specified command line as additional arguments appended. Please read the documentation of [os.system](https://docs.python.org/3/library/os.html#os.system). So the first `os.system()` results in staring `cmd.exe` and would try to change the current working directory for this instance of `cmd.exe`. That fails as `'` is interpreted as literal character by `cmd.exe`. Then the started `cmd.exe` closes itself. – Mofi Mar 05 '22 at 17:53
  • Even if the directory path would be specified correct to really change the current working directory for the started `cmd.exe`, this is of no use for the second `os.system()` as this results in starting a new `cmd.exe` with current directory being the current working directory of `python.exe` processing the Python script. So `nircmd` is again not found by `cmd.exe` when the folder path `C:\daten\super pooper\use\app\nircmd` is not part of the value of environment variable `PATH` as it is the case on Reschif's Windows computer. So that code is completely useless to solve the problem. – Mofi Mar 05 '22 at 17:57
0

Try this:

import subprocess

# Replace with the actual path to soundvolumeview.exe
soundvolumeview_path = r"C:\tools\NirLauncher\NirSoft\soundvolumeview.exe"

# Speaker name
speaker_name = "Realtek(R) Audio"

# Construct the command
command = [soundvolumeview_path, "/SetDefault", speaker_name + "\\Device\\Speakers\\Render", "all"]

# Execute the command
subprocess.run(command, shell=True)