日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python快速定位工作目录

發布時間:2023/12/31 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python快速定位工作目录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

?????? 常年奮斗在編碼一線的同學,應該都深有體會,工作久了,很多項目文件、技術資料,設計文檔,安裝包等等一堆一堆的工作目錄分散在各個磁盤中,需要用到的時候,頻繁的在各個目錄中切換,每次都得用資源瀏覽器打開,還得點多次鼠標才能找到,或者有些如Apache或者tomcat等服務又需要用命令行啟動,如果文件目錄層次比較深的話,每次重復下列動作:

開發運行->cmd->c:\\Documents and Settings\\Administrator>D:D:\>cd mycodeD:\>MyCode>cd pythonD:\>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 = 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 = 1

2.使用命令行、資源瀏覽器打開指定文件夾目錄或直接執行可執行文件?

# !/usr/bin/python2 #-*-coding:utf-8-*-3 """4 author: "wdong"5 date: @2010-8-186 summary:quick start your command file or explorer file or exec file7 """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快速定位工作目录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。