I want to partition a new, blank disk using a Python script on Ubuntu.
In a bash script or from the command line, this would do the job:
$echo -e "n\np\n1\n\n\nw\n" | sudo fdisk /dev/X
where X is the HDD in question.
I have tried to port this into a Python script using the subprocess module, as follows:
p = subprocess.Popen(cmdString, stdout=subprocess.PIPE, \
close_fds=False, stderr=subprocess.PIPE,shell=True)
stdoutAndErr = p.communicate()
where cmdString is just the same "echo -e ..." string above.
This doesn’t work though. Output is just fdisk printing out the command options, so it clearly doesn’t like what I am sending it.
What’s wrong with the above simple approach to life?
The ‘batteries included’ pipes module may be what you are looking for. Doug Hellman has a nice write-up on how to use it to get what you want.