Python快速定位工作目录
生活随笔
收集整理的這篇文章主要介紹了
Python快速定位工作目录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文鏈接:http://www.cnblogs.com/wdong/archive/2010/08/19/1802951.html
?????? 常年奮斗在編碼一線的同學,應該都深有體會,工作久了,很多項目文件、技術資料,設計文檔,安裝包等等一堆一堆的工作目錄分散在各個磁盤中,需要用到的時候,頻繁的在各個目錄中切換,每次都得用資源瀏覽器打開,還得點多次鼠標才能找到,或者有些如Apache或者tomcat等服務又需要用命令行啟動,如果文件目錄層次比較深的話,每次重復下列動作:
??? 總體思路:
??? 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 = category4 self.caption = caption5 self.path = path6 self.icon = icon7 self.command = command8 self.explorer = explorer9 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 = 12.使用命令行、資源瀏覽器打開指定文件夾目錄或直接執行可執行文件?
??? 至此,核心功能調試完畢,明天接著做圖形工具的實現,各位同行朋友,如果仔細讀完,其實接下來的實現已經很簡單了,有興趣也可以自己做個界面玩玩,同時也希望高手們多多指點
??? 我的Python工具-快速定位工作目錄(二)
總結
以上是生活随笔為你收集整理的Python快速定位工作目录的全部內容,希望文章能夠幫你解決所遇到的問題。