【问题标题】:extract the usb name from 'blkid' output从“blkid”输出中提取 USB 名称
【发布时间】:2017-08-22 19:15:37
【问题描述】:

我想提取连接到我的 linux 系统的 USB 标签。我在 python 中编写了一些代码,它工作正常,但我希望它不那么复杂。任何想法...谢谢。

代码如下:

 #!/usr/bin/env python

 import commands

 import os

 str1=commands.getoutput('sudo blkid')

 name=str1.splitlines()

 for x in range(len(name)):

 if '/dev/sd' in name[x]:

 print name[x]

 str2=name[x].split(" ")

 print str2

 for y in range(len(str2)):

 if 'LABEL' in str2[y]:

 print str2[y]

 str3=str2[y].split('=')

 print str3

 for z in range(len(str3)):

    if 'LABEL' in str3[z]:
            print str3[z+1]

【问题讨论】:

    标签: python label extract usb-drive blkid


    【解决方案1】:

    您可以使用blkid 上的选项来简化您的任务。在这种情况下-s 指定您只对LABEL 感兴趣。

    然后你可以通过反向搜索 = 字符来找到它的值,如下所示:

    out = commands.getoutput('sudo blkid -s LABEL')
    lines = out.splitlines()
    for line in lines:
         if 'dev/sd' in line:
             index = line.rfind('=')
             param = line[index + 1:]
             print param
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 2013-05-11
      • 2014-10-30
      相关资源
      最近更新 更多