This question already has answers here:
Finding only disk drives using pyudev
(2个答案)
4年前关闭。
编辑我不明白为什么这被标记为重复,因为确定的重复项需要导入pyudev。甚至接近复制。
这有效,但是感觉到“强力”。
是否有更Pythonic的方法来获取Linux上可用磁盘设备名称的列表。
使用:
(2个答案)
4年前关闭。
编辑我不明白为什么这被标记为重复,因为确定的重复项需要导入pyudev。甚至接近复制。
这有效,但是感觉到“强力”。
是否有更Pythonic的方法来获取Linux上可用磁盘设备名称的列表。
def get_list_of_available_disk_device_names():
# device names are prefixed with xvd
# any alpha characters after the prefix identify the specific device,
# it is possible that there are numbers after the fourth character
# https://rwmj.wordpress.com/2011/01/09/how-are-linux-drives-named-beyond-drive-26-devsdz/
# in this case we are hard coding the limit to an arbitrary 26 so device names do not go beyond z
# the device name prefix can vary across operating systems. 'xvd' is Xen devices on Linux
device_name_prefix = 'xvd'
device_letters = [x[3] for x in os.listdir('/dev') if x.startswith(device_name_prefix) and x[3] in string.lowercase]
device_letter_alpha_numbers = [string.lowercase.index(device_letter) for device_letter in device_letters]
next_available_device_number = max(device_letter_alpha_numbers) + 1
if next_available_device_number > 25: # a is 0, z is 25
raise Exception('No more devices available')
return ['xvd{}'.format(string.lowercase[x]) for x in range(next_available_device_number, 25)]
使用:
ubuntu@ip-x-x-x-x:~$ python tmp.py
['xvdg', 'xvdh', 'xvdi', 'xvdj', 'xvdk', 'xvdl', 'xvdm', 'xvdn', 'xvdo', 'xvdp', 'xvdq', 'xvdr', 'xvds', 'xvdt', 'xvdu', 'xvdv', 'xvdw', 'xvdx', 'xvdy']
ubuntu@ip-x-x-x-x:~$
最佳答案
我认为这可能适合。
>>> import os
>>> import os.path
>>> import string
>>> [ 'xvd' + e for e in string.ascii_lowercase if not os.path.exists('/dev/xvd' + e)]
['xvda', 'xvdb', 'xvdc', 'xvdd', 'xvde', 'xvdf', 'xvdg', 'xvdh', 'xvdi', 'xvdj', 'xvdk', 'xvdl', 'xvdm', 'xvdn', 'xvdo', 'xvdp', 'xvdq', 'xvdr', 'xvds', 'xvdt', 'xvdu', 'xvdv', 'xvdw', 'xvdx', 'xvdy', 'xvdz']
10-04 17:40