如何通过python与parted interpreter进行通信?

时间:2015-07-27 11:46:43

标签: python subprocess popen

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

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

1 个答案:

答案 0 :(得分:3)

您可以在不向儿童的标准输入发送输入的情况下实现此目的,只需使用命令行并使用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