我想要做的是創建兩個字段的字典(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
什麼讓你對這個錯誤感到困惑?設備是一個列表,你不能用字符串索引列表。 –