我想要做的是创建两个字段的字典(UUID的路径)和Ubuntu的命令行填补他们输出Python的字典创建类型错误
from subprocess import Popen, PIPE
Devices = []
def UUID():
blkid = Popen(['sudo', 'blkid'], stdout=PIPE)
sed = Popen(['sed', 's/^.*UUID="/UUID="/'], stdin=blkid.stdout, stdout=PIPE)
cut = Popen(['cut', '-d', '"', '-f', '2'], stdin=sed.stdout, stdout=PIPE)
end_of_pipe = cut.stdout
Devices = [{'uuid': uuid, 'path': None} for uuid in end_of_pipe]
return Devices
def Path(Devices):
blkid = Popen(['sudo', 'blkid'], stdout=PIPE)
cut = Popen(['cut', '-d', ':', '-f', '1'], stdin=blkid.stdout, stdout=PIPE)
end_of_pipe = cut.stdout
for path in end_of_pipe:
Devices['path'] = path
return Devices
Devices = UUID()
Path(Devices)
print Devices
返回错误:
Traceback (most recent call last):
File "2.py", line 24, in <module>
Path(Devices)
File "2.py", line 20, in Path
Devices['path'] = path
TypeError: list indices must be integers, not str
什么让你对这个错误感到困惑?设备是一个列表,你不能用字符串索引列表。 –