0

使用python替换被网络占用的驱动器号,在资源管理器中得到错误的图标

可以通过重启explorer来解决(kill and start),但是我根本不想重启

t_switch == 1用拳头运行代码,运行t_switch == 2,然后打开资源管理器,就可以找到问题了

请根据我的代码帮助解决这个问题,非常感谢

#coding=utf-8

import os
import subprocess

def win_subst(_action, _drive, _path = ''):
    if _action == 'delete':
        _cmd = 'subst /d {}'.format(_drive)
    elif _action == 'mount':
        _cmd = 'subst {} {}'.format(_drive, _path)

    p = subprocess.Popen(_cmd, shell = True)
    p.communicate()
    return p.returncode

def win_netuse(_action, _drive, _url = '', _account = '', _passwd = ''):
    _url = _url.replace('/', '\\')

    if _action == 'delete':
        _cmd = 'net use {} /delete /y'.format(_drive)
    elif _action == 'mount':
        _cmd = 'net use {} {} {} /user:{} /persistent:yes /y'.format(_drive, _url, _passwd, _account)

    p = subprocess.Popen(_cmd, shell = True)
    p.communicate()
    return p.returncode

def win_mkdir(_dir):
    if not os.path.isdir(_dir):
        _cmd = 'md {}'.format(_dir)
        subprocess.call(_cmd, shell = True)

if __name__ == '__main__':

    t_share_url = r'\\127.1\test'
    t_samba_usr = 'test'
    t_samba_pwd = 'test'
    t_win_drive = 'V:'

    t_switch    = 1

    if t_switch == 1:
        win_netuse('mount', t_win_drive, t_share_url, t_samba_usr, t_samba_pwd)
    elif t_switch == 2:
        t_share_url = r'c:\test'
        win_mkdir(t_share_url)
        win_netuse('delete', t_win_drive)
        #
        # There is a problem with next step, success to subst ,but gets wrong icon in Explorer 
        # i don't want to kill the explorer and restart it at all
        #
        win_subst('mount', t_win_drive, t_share_url)
    elif t_switch == 3:
        win_subst('delete', t_win_drive)
4

0 回答 0