'parted mklabel'通过Python子进程引发错误 [英] 'parted mklabel' raises an error through Python subprocess

查看:136
本文介绍了'parted mklabel'通过Python子进程引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用subprocess.Popen通过python脚本格式化硬盘。
在shell内键入以下命令,效果很好。只需注意以下命令即可!

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.

将其滚动到Python子进程中,Popen以状态代码1退出。
我什至无法在stdin管道中写入是。

Rolling it inside Python subprocess, Popen exits with status code 1. I'm even not able to write 'Yes' into the stdin pipe.

代码如下:

#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

或者,

# test2
p = Popen('parted /dev/sdh mklabel gpt', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)

p.stdin.write('Yes\n')

IOError :[Errno 32]管道损坏

IOError: [Errno 32] Broken pipe

我不确定Popen是否将此警告视为错误,并且在这种情况下如何更改他的行为?
感谢您的任何建议。

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.

推荐答案

通过在命令中添加-s选项,如下所示(忽略输出),

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)

这篇关于'parted mklabel'通过Python子进程引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆