问题描述
我想在 Ubuntu 上使用 Python 脚本对新的空白磁盘进行分区.
I want to partition a new, blank disk using a Python script on Ubuntu.
在 bash 脚本或命令行中,这将完成这项工作:
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
其中 X 是有问题的 HDD.
where X is the HDD in question.
我尝试使用 subprocess 模块将其移植到 Python 脚本中,如下所示:
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()
其中 cmdString
与上面的 "echo -e ..."
字符串相同.
where cmdString
is just the same "echo -e ..."
string above.
虽然这行不通.输出只是 fdisk
打印出命令选项,所以它显然不喜欢我发送的内容.
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?
推荐答案
包含电池"pipes 模块可能正是您要找的.Doug Hellman 有一篇很好的文章,介绍了如何使用它来获得您想要的东西.
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.
这篇关于如何在 Python 子进程模块中使用管道命令字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!