Reputation: 591
I'd like to run an fdisk function in python, but the returns are making this not work...
command = ['echo', '-e', "'o\nn\np\n1\n\n\nw'", '|', 'sudo', 'fdisk', '/dev/xvdm']
p = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, err = p.communicate()
This gives the the (incorrect) output of:
b"'o\nn\np\n1\n\n\nw' | sudo fdisk /dev/xvdm\n"
What is the equivalent?
Upvotes: 0
Views: 831
Reputation: 23
You can not use a pipe (|) in a command like this. The pipe is given as argument to the program ("echo" in your case).
scnerd give you the best way/answer to send input text to fdisk.
If you really want to keep the pipe, you shall run a "bash" program with argument "-c" (command), and give in parameter the command (including your pipe) :
command = ['bash', '-c', "echo -e 'o\nn\np\n1\n\n\nw' | sudo fdisk /dev/xvdm"]
p = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, err = p.communicate()
Upvotes: 1
Reputation: 6103
Why not just run fdisk
and send it the input yourself?
command = ['sudo', 'fdisk', '/dev/xvdm']
p = subprocess.Popen(command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, err = p.communicate(b"o\nn\np\n1\n\n\nw")
Upvotes: 2