1

Execute CMD Command in python after opening CMD with specific directory

Opening CMD with specific directory using python. In the same sequence, have to execute below CMD Command using python.

Step 2 :

py <filename> –V <variant string> -p <path to the flash files>

Step 1 :

import os
os.system("start cmd /K cd C:\\Users\\Desktop\\Folder\\File" )
phuclv
  • 37,963
  • 15
  • 156
  • 475
Suji Ryali
  • 65
  • 2
  • 5

2 Answers2

5

What you can do is that you have to change your working directory first before executing the code.

import os

os.chdir('C:\\Users\\Name\\Desktop\\testing')
os.system("start cmd /K py <filename> –V <variant string> -p <path to the flash files>")
Axois
  • 1,961
  • 2
  • 11
  • 22
  • Thank You :-) But flashing didn't start. May be it's due to, not opening CMD as Administrator. How to modify script to run CMD as administrator and execute files – Suji Ryali Aug 05 '19 at 06:55
  • @SujiRyali you can probably take a look at [this](https://stackoverflow.com/questions/130763/request-uac-elevation-from-within-a-python-script/11746382#11746382). I am not really familiar with `ctypes` and the whole windows thing, so I'm afraid i cant help much. – Axois Aug 05 '19 at 07:04
  • Well it actually worked with code you have given. Thanks a lot :-) – Suji Ryali Aug 05 '19 at 07:51
0

you can do so using subprocess module , refer this tutorial for executing your commands.It works for windows too.

Rangeesh
  • 361
  • 1
  • 13