using python to subst drive letter which be occupied by network, gets wrong icon in explorer

KuoHsing Lan 1 Reputation point
2021-10-11T01:50:08.467+00:00

Can be solved by restarting explorer(kill and start),but i don't want to restart it at all

run code with t_switch == 1 fist,and run t_switch == 2,then open the explorer,you can find the problem

Please help solve this problem based on my code, thanks a lot

#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
        #
        # and i used to tried separately umap a network drive in cmd, Explorer will refresh automatically,
        # the drive letter will be release(the icon disapper) immediately, then subst a local directory,
        # this will not cause any problems 
        #
        win_subst('mount', t_win_drive, t_share_url)
    elif t_switch == 3:
        win_subst('delete', t_win_drive)
Community Center | Not monitored
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.