{"id":964647,"date":"2024-12-27T04:30:19","date_gmt":"2024-12-26T20:30:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/964647.html"},"modified":"2024-12-27T04:30:21","modified_gmt":"2024-12-26T20:30:21","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e7%a1%ac%e7%9b%98id","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/964647.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u786c\u76d8id"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181335\/4b82bac1-fb42-45c9-954a-59dd8dbbcf33.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u786c\u76d8id\" \/><\/p>\n<p><p> <strong>\u8981\u83b7\u53d6\u786c\u76d8ID\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>os<\/code>\u3001<code>subprocess<\/code>\u6216<code>psutil<\/code>\uff0c\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u6216\u76f4\u63a5\u8bbf\u95ee\u786c\u4ef6\u4fe1\u606f\u3002\u901a\u8fc7\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u3001\u4f7f\u7528WMI\uff08Windows Management Instrumentation\uff09\u548c\u8bfb\u53d6\u78c1\u76d8\u5e8f\u5217\u53f7\u7b49\u65b9\u6cd5\uff0c\u60a8\u80fd\u591f\u83b7\u53d6\u786c\u76d8\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002<\/strong>\u5728\u8fd9\u51e0\u79cd\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528WMI\u662f\u83b7\u53d6\u786c\u76d8\u4fe1\u606f\u7684\u4e00\u4e2a\u5e38\u89c1\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7cfb\u7edf\u7ba1\u7406\u4fe1\u606f\u7684\u7edf\u4e00\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u901a\u8fc7WMI\u63a5\u53e3\u5728Windows\u4e0a\u83b7\u53d6\u786c\u76d8ID\u7684\u8be6\u7ec6\u89e3\u91ca\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528WMI\u83b7\u53d6\u786c\u76d8ID<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u6240\u9700\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5<code>wmi<\/code>\u5e93\uff0c\u8fd9\u4e2a\u5e93\u5141\u8bb8Python\u4e0eWindows\u7684WMI\u63a5\u53e3\u8fdb\u884c\u4ea4\u4e92\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install WMI<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u4e00\u65e6\u5b89\u88c5\u4e86\u5e93\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2aPython\u811a\u672c\u6765\u83b7\u53d6\u786c\u76d8ID\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import wmi<\/p>\n<p>def get_hard_drive_id():<\/p>\n<p>    c = wmi.WMI()<\/p>\n<p>    for disk in c.Win32_DiskDrive():<\/p>\n<p>        print(f&quot;Disk: {disk.Caption}, Serial Number: {disk.SerialNumber}&quot;)<\/p>\n<p>get_hard_drive_id()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u811a\u672c\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aWMI\u5bf9\u8c61\uff0c\u7136\u540e\u904d\u5386<code>Win32_DiskDrive<\/code>\u7c7b\u7684\u5b9e\u4f8b\u4ee5\u83b7\u53d6\u786c\u76d8\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5305\u62ec\u786c\u76d8ID\u548c\u5e8f\u5217\u53f7\u3002<\/p>\n<\/p>\n<p><h4>3. \u89e3\u91ca\u4ee3\u7801<\/h4>\n<\/p>\n<ul>\n<li><strong>wmi.WMI()<\/strong>: \u521b\u5efa\u4e00\u4e2aWMI\u5bf9\u8c61\uff0c\u5b83\u662f\u4e0eWindows Management Instrumentation\u8fdb\u884c\u4ea4\u4e92\u7684\u4e3b\u8981\u5165\u53e3\u3002<\/li>\n<li><strong>c.Win32_DiskDrive()<\/strong>: \u8bbf\u95eeWin32_DiskDrive\u7c7b\u7684\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u7c7b\u5305\u542b\u8ba1\u7b97\u673a\u4e2d\u6240\u6709\u786c\u76d8\u7684\u76f8\u5173\u4fe1\u606f\u3002<\/li>\n<li><strong>disk.SerialNumber<\/strong>: \u8fd9\u662f\u786c\u76d8\u7684\u5e8f\u5217\u53f7\uff0c\u5373\u786c\u76d8ID\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u5728Linux\u4e0a\u83b7\u53d6\u786c\u76d8ID<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u5728Linux\u4e0a\u5de5\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6267\u884cshell\u547d\u4ee4\u6765\u83b7\u53d6\u786c\u76d8ID\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def get_hard_drive_id():<\/p>\n<p>    try:<\/p>\n<p>        result = subprocess.run([&#39;lsblk&#39;, &#39;-o&#39;, &#39;NAME,SERIAL&#39;], capture_output=True, text=True, check=True)<\/p>\n<p>        print(result.stdout)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>get_hard_drive_id()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u89e3\u91ca\u4ee3\u7801<\/h4>\n<\/p>\n<ul>\n<li><strong>subprocess.run()<\/strong>: \u6267\u884cshell\u547d\u4ee4<code>lsblk<\/code>\uff0c\u8fd9\u4e2a\u547d\u4ee4\u5217\u51fa\u6240\u6709\u5757\u8bbe\u5907\u7684\u540d\u79f0\u548c\u5e8f\u5217\u53f7\u3002<\/li>\n<li><strong>capture_output=True<\/strong>: \u6355\u83b7\u547d\u4ee4\u6267\u884c\u7684\u8f93\u51fa\u3002<\/li>\n<li><strong>text=True<\/strong>: \u5c06\u8f93\u51fa\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5904\u7406\u3002<\/li>\n<li><strong>check=True<\/strong>: \u5982\u679c\u547d\u4ee4\u6267\u884c\u8fd4\u56de\u975e\u96f6\u72b6\u6001\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>psutil<\/code>\u5e93\u8de8\u5e73\u53f0\u83b7\u53d6\u786c\u76d8\u4fe1\u606f<\/h3>\n<\/p>\n<p><p><code>psutil<\/code>\u5e93\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u5e93\uff0c\u53ef\u4ee5\u5728Windows\u3001Linux\u548cmacOS\u4e0a\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u6240\u9700\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install psutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import psutil<\/p>\n<p>def get_hard_drive_info():<\/p>\n<p>    partitions = psutil.disk_partitions()<\/p>\n<p>    for partition in partitions:<\/p>\n<p>        print(f&quot;Device: {partition.device}, Mountpoint: {partition.mountpoint}, File system type: {partition.fstype}&quot;)<\/p>\n<p>get_hard_drive_info()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u89e3\u91ca\u4ee3\u7801<\/h4>\n<\/p>\n<ul>\n<li><strong>psutil.disk_partitions()<\/strong>: \u83b7\u53d6\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u5206\u533a\u4fe1\u606f\u3002<\/li>\n<li><strong>partition.device<\/strong>: \u786c\u76d8\u8bbe\u5907\u540d\u79f0\u3002<\/li>\n<li><strong>partition.mountpoint<\/strong>: \u6302\u8f7d\u70b9\u3002<\/li>\n<li><strong>partition.fstype<\/strong>: \u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u786c\u76d8ID\u5728\u786c\u4ef6\u4fe1\u606f\u91c7\u96c6\u3001\u7cfb\u7edf\u7ba1\u7406\u548c\u8f6f\u4ef6\u6388\u6743\u7b49\u9886\u57df\u975e\u5e38\u6709\u7528\u3002\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e0d\u540c\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u786c\u76d8ID\u3002\u5bf9\u4e8eWindows\u7528\u6237\uff0cWMI\u662f\u4e00\u4e2a\u53ef\u9760\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u9009\u62e9\uff1b\u800c\u5bf9\u4e8eLinux\u7528\u6237\uff0c\u53ef\u4ee5\u5229\u7528<code>subprocess<\/code>\u6267\u884c\u547d\u4ee4\u6216\u4f7f\u7528<code>psutil<\/code>\u5e93\u5b9e\u73b0\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u51c6\u786e\u5730\u83b7\u53d6\u786c\u76d8\u7684\u552f\u4e00\u6807\u8bc6\u4fe1\u606f\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u786c\u76d8\u7684\u552f\u4e00\u6807\u8bc6\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u8fd0\u884c\u7cfb\u7edf\u547d\u4ee4\u6765\u83b7\u53d6\u786c\u76d8ID\u3002\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>wmic diskdrive get serialnumber<\/code>\u547d\u4ee4\uff0c\u800c\u5728Linux\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>lsblk -o SERIAL<\/code>\u6216<code>udevadm info --query=property --name=\/dev\/sda | grep ID_SERIAL<\/code>\u7b49\u547d\u4ee4\u3002\u8fd9\u4e9b\u547d\u4ee4\u4f1a\u8fd4\u56de\u786c\u76d8\u7684\u5e8f\u5217\u53f7\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7Python\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u8f93\u51fa\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u83b7\u53d6\u786c\u76d8ID\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>psutil<\/code>\u5e93\u6765\u83b7\u53d6\u786c\u76d8\u7684\u76f8\u5173\u4fe1\u606f\u3002\u901a\u8fc7\u8c03\u7528<code>psutil.disk_partitions()<\/code>\u548c<code>psutil.disk_usage()<\/code>\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u83b7\u53d6\u78c1\u76d8\u7684\u6302\u8f7d\u70b9\u53ca\u4f7f\u7528\u60c5\u51b5\uff0c\u867d\u7136\u5b83\u4e0d\u76f4\u63a5\u63d0\u4f9b\u786c\u76d8ID\uff0c\u4f46\u53ef\u4ee5\u4e0e\u5176\u4ed6\u7cfb\u7edf\u547d\u4ee4\u7ed3\u5408\u4f7f\u7528\uff0c\u83b7\u53d6\u66f4\u5168\u9762\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u83b7\u53d6\u786c\u76d8ID\u662f\u5426\u9700\u8981\u7279\u5b9a\u7684\u6743\u9650\uff1f<\/strong><br \/>\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u83b7\u53d6\u786c\u76d8ID\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002\u4f8b\u5982\uff0c\u5728Windows\u4e0a\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884cPython\u811a\u672c\uff0c\u624d\u80fd\u6210\u529f\u6267\u884c<code>wmic<\/code>\u547d\u4ee4\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u67d0\u4e9b\u547d\u4ee4\u53ef\u80fd\u9700\u8981sudo\u6743\u9650\u3002\u786e\u4fdd\u5728\u8fd0\u884c\u811a\u672c\u4e4b\u524d\u68c0\u67e5\u76f8\u5173\u6743\u9650\u8bbe\u7f6e\uff0c\u4ee5\u907f\u514d\u6743\u9650\u4e0d\u8db3\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u83b7\u53d6\u786c\u76d8ID\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982os\u3001subprocess\u6216psutil\uff0c\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9 [&hellip;]","protected":false},"author":3,"featured_media":964653,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964647"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=964647"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964647\/revisions"}],"predecessor-version":[{"id":964654,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964647\/revisions\/964654"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/964653"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=964647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=964647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=964647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}