I'm trying to access the serial_number
key of the first _items
array in the generated plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>_SPCommandLineArguments</key>
<array>
<string>/usr/sbin/system_profiler</string>
<string>-nospawn</string>
<string>-xml</string>
<string>SPHardwareDataType</string>
<string>-detailLevel</string>
<string>full</string>
</array>
<key>_SPCompletionInterval</key>
<real>0.15617406368255615</real>
<key>_SPResponseTime</key>
<real>0.18982398509979248</real>
<key>_dataType</key>
<string>SPHardwareDataType</string>
<key>_detailLevel</key>
<string>-2</string>
<key>_items</key>
<array>
<dict>
<key>serial_number</key>
<string>C0**</string>
</dict>
</array>
</dict>
</array>
</plist>
Here is the Python code:
def get_serial_number():
import plistlib
cmd = ['system_profiler', 'SPHardwareDataType', '-xml']
plist = subprocess.check_output(cmd)
with open('hardware.plist', 'wb') as fp:
plistlib.dump(plist, fp)
with open('hardware.plist', 'rb') as fp:
pl = plistlib.load(fp, fmt=None, dict_type=dict)
serial = pl[0]['_items'][0]['serial_number']
return serial
The error I receive is: 'int' object is not subscriptable
. I'm assuming this is because of the way I'm trying to access the keys of the plist on line 9, but I cannot figure it out.
Any assistance would be greatly appreciated!
That was lots of fun to work out! I think you want this to get the serial number from a system_profiler
plist
You can also do it without
plistlib
like this: