如何通过 python 与 parted 解释器通信?

How to communicate with parted interpreter via python?

from subprocess import Popen,PIPE,STDOUT

a=Popen('parted -s',stdout = PIPE, stderr = STDOUT, shell = True)
a.communicate(input="print".encode())
string=a.stdout.read()

我想在 parted 中使用 "print" 命令,我该怎么做?

您无需向 child 的标准输入发送输入即可实现此目的,只需使用命令行并使用 subprocess.check_output():

执行即可
import shlex
import subprocess

DEVICE = '/dev/sda'
cmd = 'parted -s {} print'.format(DEVICE)

output = subprocess.check_output(shlex.split(cmd))

>>> print output
Model: ATA Hitachi HTS54756 (scsi)
Disk /dev/sda: 640GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags: 

Number  Start   End    Size   Type      File system  Flags
 1      1049kB  210MB  209MB  primary   ntfs         boot
 2      210MB   322GB  322GB  primary   ntfs
 4      322GB   640GB  318GB  extended
 5      322GB   322GB  524MB  logical   ext4
 6      322GB   640GB  318GB  logical                lvm
 3      640GB   640GB  108MB  primary   fat32        lba