'parted mklabel' raises an error through Python subprocess

被刻印的时光 ゝ 提交于 2019-12-12 03:44:47

问题


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()

1

Or,

# 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.


回答1:


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)


来源:https://stackoverflow.com/questions/14770738/parted-mklabel-raises-an-error-through-python-subprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!