快速切换目录工具c之windows版本

#!/usr/bin/env python

import os,sys
#import fcntl,termios
import winput
from winput.vk_codes import *

dirlstfile = "\\bin\\.dirlst"
dirlst = open(dirlstfile, "r+").read().split("\n")[:-1]
updated = 0

def listdir(dirlst, filter):
    startc = 'a'
    lastc = 'a'
    dircnt = 0
    for item in dirlst:
        if item!='' and filter in item:
            print(startc,item)
            lastc = startc
            dircnt+=1
        startc=chr(ord(startc)+1)
    if dircnt == 0 :
        print("use 'c +' to add dir cache")
        return
    if dircnt == 1:
        seldir(dirlst, lastc)
        return
    ret = input("select:")
    if ret == '':return
    seldir(dirlst, ret[0])

def shellcmd(cmd):
    #for c in cmd:fcntl.ioctl(0, termios.TIOCSTI,c)
    for c in cmd:
        val = ord(c)

        if c==':':
            winput.press_key(VK_SHIFT)
            winput.click_key(0xBA)
            winput.release_key(VK_SHIFT)
            continue

        if c=='\\':
            winput.click_key(0xE2)
            continue

        if val==0x0a:
            winput.click_key(0x0D)
            continue

        if c=='/':
            winput.click_key(0xBF)
            continue

        if c=='.':
            winput.click_key(0xBE)
            continue

        if val>= 0x40 and val<0x60:
            winput.press_key(VK_SHIFT)
            winput.click_key(val)
            winput.release_key(VK_SHIFT)
            continue

        if val>= 0x60 and val<0x80:
            val &= ~0x20

        winput.click_key(val)
    winput.stop()

def seldir(dirlst, c):
    #print("cd %s\n"%(dirlst[ord(c)-ord('a')]))
    shellcmd("cd %s\n"%(dirlst[ord(c)-ord('a')]))

if len(sys.argv) > 1:
    if sys.argv[1] == '-':
        updated = 1
        if len(sys.argv) == 2:
            if os.getcwd() in dirlst:
                print("remove self")
                dirlst.remove(os.getcwd())
        else:
            startc = 'a'
            dirbak = dirlst.copy()
            for item in dirlst:
                if startc in sys.argv[2]: dirbak.remove(item)
                startc=chr(ord(startc)+1)
            dirlst = dirbak.copy()

    elif sys.argv[1] == '+':
        if os.getcwd() not in dirlst:
            dirlst.append(os.getcwd())
            for item in dirlst:
                if item=="":
                    item=os.getcwd()
                    dirlst=dirlst[:-1]
                    break
            updated = 1
    elif '1' <= sys.argv[1][0] <= '9' and len(sys.argv[1]) == 1:
        shellcmd("cd %s\n"%('../'*(ord(sys.argv[1])-ord('0'))))
    elif len(sys.argv[1]) == 1:
        seldir(dirlst, sys.argv[1])
    else:
        listdir(dirlst, sys.argv[1])

else:
    listdir(dirlst, "")

if updated == 1:
    fl = open(dirlstfile, "w")
    for item in dirlst:
        fl.write("%s\n"%item)

效果截图,切换任意目录,只需要一行3~4个字符的按键命令。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值