I am looking to create a diskpart script for Windows using Python.
I need to run diskpart and then issues additional commands after the program is executed the series of inputs are below. I will eventually put this in a loop so it can be done for a range of disks.
- SELECT DISK 1
- ATTRIBUTES DISK CLEAR READONLY
- ONLINE DISK noerr
- CLEAN
- CREATE PART PRI
- SELECT PART 1
- ASSIGN
- FORMAT FS=NTFS QUICK LABEL="New Volume"
I have tried to do this as follows below.
In the example below I am able to execute diskpart then run the first command which is "select disk 1" and then it terminates. I want to be able to send the additional commands to complete the process of preparing the disk how can this be done? diskpart does not take arguments that can facilitate this besides reading from a file but I want to avoid that on Windows 2012 PowerShell cmdelts make this easier to achieve.
import subprocess
from subprocess import Popen, PIPE, STDOUT
p = Popen(['diskpart'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
grep_stdout = p.communicate(input=b'select disk 1')[0]
print(grep_stdout.decode())
Looking for something along the lines of
from subprocess import Popen, PIPE, STDOUT
p = Popen(['diskpart'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
grep_stdout = p.communicate(input=b'select disk 1')[0]
- run command
- run command
- run command
- run command
- run command
- run command
- run command
print(grep_stdout.decode())
I tried the following below and actually executes diskpart and then also runs the command "select disk 1" and exits after that I belive this is not the correct way of sending the input but is more along the lines of what I am trying to achieve if I can continue to send subsequent commands.
import subprocess
from subprocess import Popen, PIPE, STDOUT
p = Popen(['diskpart'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
grep_stdout = p.communicate(input=b'select disk 1')[0]