原文链接:http://www.cnblogs.com/wdong/archive/2010/08/19/1802951.html

常年奋斗在编码一线的同学,应该都深有体会,工作久了,很多项目文件、技术资料,设计文档,安装包等等一堆一堆的工作目录分散在各个磁盘中,需要用到的时候,频繁的在各个目录中切换,每次都得用资源浏览器打开,还得点多次鼠标才能找到,或者有些如Apache或者tomcat等服务又需要用命令行启动,如果文件目录层次比较深的话,每次重复下列动作:

开发运行->cmd->c:\\Documents and Settings\\Administrator>D:
D:\>cd mycode
D:\>MyCode>cd python
D:\>MyCode>Python>Practise>...

反反复复的重复动作真的是太烦了,自己动手丰衣足食,还是用代码自己来写一段快速定位或切换工作目录的小工具,像工具软件一样点一下就直接打开,岂不是省去敲打那些无聊的cd命令,ok,因为已经凌晨了,突然来了兴致即兴发挥,实在搞不完,这一篇写把总体思路及几个关键的核心代码实现掉,下一篇完成所有功能

    总体思路:

    1.软件中可自定义常用工作目录或文件的配置项

    2.选择某一工作目录项可打开命令行,路径直接指向到指定的文件夹处

    3.选择某一工作目录项可打开资源浏览器,路径直接指向到指定的文件夹处

    4.选择某一文件项可直接打开执行

    5.使用python + Qt实现图形化操作界面(基于托盘图标)的工具软件

此篇实现前四个核心功能需求

1.使用python ConfigParser实现可配置项

代码:

 class ConfigItem(object):
2 def __init__(self,category,caption,path,icon,command, explorer, execute):
3 self.category = category
4 self.caption = caption
5 self.path = path
6 self.icon = icon
7 self.command = command
8 self.explorer = explorer
9 self.execute = execute
10
11 def readConfigItems(file,sect=None,key=None):
12 if not os.path.isfile(file):
13 raise Exception("FileNotFound")
14 cf = ConfigParser.ConfigParser()
15 cf.read(file)
16 sections = cf.items("sections")
17
18 if sect is None and key is None:
19 l = []
20 for name,key in sections:
21 i = ConfigItem(
22 category= cf.get(key,"category"),
23 caption=cf.get(key,"caption"),
24 path=cf.get(key,"path"),
25 icon=cf.get(key,"icon"),
26 command=cf.getboolean(key,"command"),
27 explorer=cf.getboolean(key,"explorer"),
28 execute=cf.getboolean(key,"execute")
29 )
30 l.append(i)
31 return l
32 return cf.get(sect,key)
33
34 def addConfigItem(file,item):
35 config = ConfigParser.RawConfigParser()
36 if not os.path.isfile(file):
37 raise Exception("FileNotFound")
38 config = ConfigParser.ConfigParser()
39 config.read(file)
40 sections = config.items("sections")
41 new_section = "section" + str(len(sections)+1)
42 config.set("sections",new_section,new_section)
43 config.add_section(new_section)
44 config.set(new_section, 'category', item.category)
45 config.set(new_section, 'caption', item.caption)
46 config.set(new_section, 'path', item.path)
47 config.set(new_section, 'icon', item.icon)
48 config.set(new_section, 'command',item.command)
49 config.set(new_section, 'explorer', item.explorer)
50 config.set(new_section, 'execute', item.execute)
51 # Writing our configuration file to 'example.cfg'
52 with open(file, 'wb') as configfile:
53 config.write(configfile)
54
55
56 if __name__ == "__main__":
57 #os.execvp(file)
58 #openCommand(r"D:\MyCode\Python\Pratices")
59 #openExplorer(r"D:\MyCode\Python\Pratices")
60 item = ConfigItem(
61 category= "3",
62 caption="test",
63 path=1,
64 icon=1,
65 command=True,
66 explorer=True,
67 execute=True
68 )
69 addConfigItem("d:\\conf.conf", item)
70 print readConfigItems("d:\\conf.conf")

配置文件格式:

[sections]
section2 = section2
section1 = section1 [section2]
category = 1
execute = False
explorer = True
caption = 工作目录
command = True
path = 1
icon = 1 [section1]
category = 工作目录
execute = True
explorer = True
caption = Python练习代码
command = True
path = 1
icon = 1

2.使用命令行、资源浏览器打开指定文件夹目录或直接执行可执行文件

# !/usr/bin/python
2 #-*-coding:utf-8-*-
3 """
4 author: "wdong"
5 date: @2010-8-18
6 summary:quick start your command file or explorer file or exec file
7 """
8
9 import os, time, datetime,sys
10 import ConfigParser
11
12 def openCommand(dest):
13 """
14 Specify the folder to opened by command line
15 """
16 cmd = "cmd /k cd \"" + dest + "\""
17 import subprocess
18 subprocess.Popen(cmd, shell=True)
19
20 def openExplorer(dest):
21 """
22 Specify the folder to opened by Explorer
23 """
24 cmd = "explorer \"" + dest + "\""
25 import subprocess
26 subprocess.Popen(cmd, shell=False)
27
28 def execFile(file):
29 """
30 execute the file by system register file type
31 """
32 if os.path.isfile(path):
33 try:
34 os.system(file)
35 except:
36 os.system("file:///" + str(file).replace("\\\\", "/"))
37 else:
38 openExplorer(file)

至此,核心功能调试完毕,明天接着做图形工具的实现,各位同行朋友,如果仔细读完,其实接下来的实现已经很简单了,有兴趣也可以自己做个界面玩玩,同时也希望高手们多多指点

我的Python工具-快速定位工作目录(二)

Python快速定位工作目录的更多相关文章

  1. python获得当前工作目录和修改

    import os  curDir = os.getcwd() 最近使用Python 写了很多脚本,想导入脚本,发现不知道如何查看python 的默认工作目录,并修改默认工作目录. 方法/步骤   查 ...

  2. Python 改变当前工作目录

    在shell里编辑: import os os.getcwd()   #取得当前工作目录 os.chdir(r'目标目录')   # os.chdir(r'd:\learnpy')

  3. Python改变当前工作目录

    import os print(os.getcwd()) # 打印当前工作目录 os.chdir('/Users/<username>/Desktop/') # 将当前工作目录改变为`/U ...

  4. python获取当前工作目录

    py文件所在位置/test/pj/hello.py 用户所在位置:/ 用户执行命令python /test/pj/hello.py 1. os.getcwd() 返回的是执行命令的位置 / 2.sys ...

  5. python中修改工作目录

  6. 第14.12节 Python中使用BeautifulSoup解析http报文:使用select方法快速定位内容

    一. 引言 在<第14.10节 Python中使用BeautifulSoup解析http报文:html标签相关属性的访问>和<第14.11节 Python中使用BeautifulSo ...

  7. 第14.11节 Python中使用BeautifulSoup解析http报文:使用查找方法快速定位内容

    一. 引言 在<第14.10节 Python中使用BeautifulSoup解析http报文:html标签相关属性的访问>介绍了BeautifulSoup对象的主要属性,通过这些属性可以访 ...

  8. 20180129周一之学习PYTHON笔记【安装、查看工作目录、】

    一,安装过程中多选一个ADD的项,免去设置环境变量. 二,PYAUTOGUI模块控制键鼠. IMAGE模块. ----------------------python 如何查看与更换工作目录----- ...

  9. 「Python调试器」,快速定位各种疑难杂症!!

    现在很多的编辑器其实都带着「调试程序」的功能,比如写 c/c++ 的 codeblocks,写 Python 的 pycharm,这种图形界面的使用和显示都相当友好,简单方便易学,这个不是我这篇文章要 ...

随机推荐

  1. Python学习【第1篇】:环境配置

    1. 下载安装包 https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi    # 2.7安装包   https://www. ...

  2. 自定义数据类型使用QVariant转换的方法

    QVariant类型的放入和取出必须是相对应的,你放入一个int就必须按int取出,不能用toString(), Qt不会帮你自动转换. 数据核心无非就是一个 union,和一个标记类型的type:传 ...

  3. Intro.js 分步向导插件使用方法

    简介 为您的网站和项目提供一步一步的.更好的介绍 Intro.js 目前兼容 Firefox.Chrome 和 IE8,不兼容 IE6 和 IE7,后续版本将会提供更好的兼容. 在线演示及下载 在线演 ...

  4. Silverlight之我见——制作星星闪烁动画

    圣诞节来了,无聊,做点东西纪念一下. 原理很简单,生成1000个圆,从随机数来布置它们的位置,通过动画来处理它们的透明度,动画时长也是随机生成. 1.创建图形数组并设置背景透明,渐变笔触,大小等,而后 ...

  5. 权值线段树&线段树合并

    权值线段树 所谓权值线段树,就是一种维护值而非下标的线段树,我个人倾向于称呼它为值域线段树. 举个栗子:对于一个给定的数组,普通线段树可以维护某个子数组中数的和,而权值线段树可以维护某个区间内数组元素 ...

  6. hdu 1829 带权并查集的运用类似于食物链但是更简单些

    #include<stdio.h> #define N 1100000 struct node { int x,y; }f[N],pre[N]; int find(int x) { if( ...

  7. 【ACM】hdu_1094_A+BVI_201307261731

    A+B for Input-Output Practice (VI)Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276 ...

  8. [bzoj3209]花神的数论题_数位dp

    花神的数论题 bzoj-3209 题目大意:sum(i)表示i的二进制表示中1的个数,求$\prod\limits_{i=1}^n sum(i)$ 注释:$1\le n\le 10^{15}$. 想法 ...

  9. HDU 3644

    模拟退火算法.... 这道题,呃.我怎么感觉他就是随机的.同一个代码,时而AC,时而WA.其实还真的是随机的.呵呵呵呵呵...因为下降火太快了,没办法,而降得慢又会...TLE,虽然精度提高了. 敢问 ...

  10. Cocos Code IDE里xcodeprojectlua脚本更新

    lua脚本改动后xcode须要clean又一次编译才干更新,这个是xcode里的老毛病了,网上有一些脚本但不是针对Cocos Code IDE的project文件夹的,这里列出 cocos2dx版本号 ...