I want to format a hard disk via python script using subprocess.Popen. Typing the following command inside a shell worls fine. Just pay attention with this command!
parted /dev/sdh mklabel gpt Warning: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No?
I agree by typing Yes and the disk is well formatted.
Rolling it inside Python subprocess, Popen exits with status code 1. I'm even not able to write 'Yes' into the stdin pipe.
Code as follows:
#test1 from subprocess import Popen, PIPE, STDOUT p = Popen('parted /dev/sdh mklabel gpt', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT) print p.wait()
# test2 p = Popen('parted /dev/sdh mklabel gpt', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT) p.stdin.write('Yes\n')
IOError: [Errno 32] Broken pipe
I'm not sure if Popen does not consider this warning as an error, and how could I change his behaviour if this is the case? Thanks for any suggestions.Answer1:
By adding the -s option to the command as follows(ignore output), parted exits with success.
Popen('parted -s /dev/sdh mklabel gpt', shell=True)