返回列表 发帖

[技术讨论] python3 ubuntu 自动挂载u盘

#!/usr/bin/env python3
#coding=utf-8
import os
import re
import shutil
def mount_u():
    '''
    # mount U Disk to /mnt
    '''
    os.system('echo "\033[32mMount U Disk To /mnt\033[0m"')
    if not os.path.isdir(r'/mnt'): os.mkdir(r'/mnt')
    #check u disk is mount to mnt
    if os.system('mountpoint -q /mnt') == 0:
        print('/mnt is exist, umount /mnt')
        os.system('umount /mnt')
    fdisk = os.popen('fdisk -l')
    mode = re.compile(r'/dev/sd[b-z]1')
    u_path = re.search(mode, fdisk.read())
    if u_path:
        print('U Disk: {}'.format(u_path.group()))
        if os.system('mount ' + u_path.group() + ' /mnt') == 0:
            print('U Disk mounted successfully')
        else:
            print('U Disk mounting failed')
    else:
        print('U Disk: {}'.format('NO Find!'))
if __name__ == '__main__':
    mount_u()COPY

返回列表