1

I’m writing a script which needs to get mainboard serial number (on Windows) by

wmic baseboard get serialnumber

in cmd which means I have to use the following command in python:

sr = subprocess.check_output('wmic baseboard get serialnumber')

but, since the current directory of a target PC cmd may be different to the directory containing the wmic.exe to execute, first of all I have to change the cmd directory to

C:\Windows\SysWOW64\wbem

which is executed by the following command in cmd:

cd C:\Windows\SysWOW64\wbem

But, how can I run late cmd command through python script?

Mohammadreza
  • 79
  • 2
  • 9
  • Why do you want to do that? Why not call the process directly? – Nico Haase Aug 01 '19 at 13:27
  • @ Nico Haase because it results in this error: "wmic is not recognized as an internal or external command, operable program or batch file". And it's because the current directory may be different to the directory containing the file to execute. – Mohammadreza Aug 01 '19 at 13:45
  • It is not necessary to make the directory of an executable the current directory for the running process to be able to run the executable. Good coded executables to which wimc.exe from Microsoft belongs can be executed from any directory using full qualified file name which means running the executable with drive + path + file name + file extension which is `%SystemRoot%\System32\wbem\wmic.exe` as I wrote already today on your previous question. `%SystemRoot%` references the value of predefined Windows environment variable `SystemRoot` containing path to Windows directory. – Mofi Aug 01 '19 at 14:58
  • So you can write your Python script to [get value of environment variable](https://stackoverflow.com/questions/4906977/) `SystemRoot`, concatenate this folder path string with `\System32\wbem\wmic.exe` and use this concatenated full qualified file name to run `wmic.exe` from within your Python script. Please note that `wmic.exe` outputs data always UTF-16 Little Endian encoded which must be taken into account on capturing this output from within the Python script. – Mofi Aug 01 '19 at 15:05
  • @Mofi The problem is that sr = subprocess.check_output('wmic baseboard get serialnumber') is executed in python shell on another pc but not on this pc!!! – Mohammadreza Aug 01 '19 at 15:07
  • And I strongly recommend to let in this case [Windows File System Redirector](https://learn.microsoft.com/en-us/windows/desktop/WinProg64/file-system-redirector) decide if executed is 64 bit `wmic.exe` in ``%SystemRoot%\System32\wbem\`` on 64 bit Windows or 32 bit `wmic.exe` in ``%SystemRoot%\SysWOW64\wbem\`` on 64 bit Windows respectively in ``%SystemRoot%\System32\wbem\`` on 32 bit Windows. The hard coded usage of `%SystemRoot%\SysWOW64\wbem\wmic.exe` would result in Python script not working on Windows x86. – Mofi Aug 01 '19 at 15:08

3 Answers3

1

You can use the cwd argument:

p = subprocess.Popen([command, argument1,...], cwd='C:\Windows\SysWOW64\wbem')

If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd.

These may be helpful:

0

This is for opening command prompt with specific directory

import os os.system("start cmd /K cd C:\Users\Desktop\folder\file" )

Suji Ryali
  • 65
  • 2
  • 5
  • I used it before sr = subprocess.check_output('wmic baseboard get serialnumber') in my script as you suggested by it only opens cmd windows in desired directory waiting for the command and the subprocess command is not executed in python! – Mohammadreza Aug 01 '19 at 13:42
0

Check this

from os import system
system('cd <your destination>')

Or this

from os import chdir
chdir('<your destination>')
Delthis = chdir('<your destination>')
del Delthis #delete that zero
Fat Frog
  • 5
  • 8