歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python中OS模塊的常用接口和異常中Exception的運用

Python中OS模塊的常用接口和異常中Exception的運用

日期:2017/3/1 9:13:26   编辑:Linux編程

1、os.path.join(arg1, arg2)

將arg1和arg2對應的字符串連接起來並返回連接後的字符串,如果arg1、arg2為變量,就先將arg1、arg2轉換為字符串後再進行連接。

2、self.settings = Gio.Settings.new("com.linuxmint.mintmenu")

  在默認環境路徑中,根據“com.linuxmint.mintmenu”開頭的XML配置文件創建配置變量self.settings,通常XML配置文件位於/usr/share/glib-2.0/schemas/該目錄下,主要包括插件的列表及插件本身屬性鍵對應的鍵值,包括面的寬度、高度、顏色、圖標大小、分類、粘性(sticky)和關鍵字本身屬性的類型默認定義等。

  然後在代碼中可以根據配置變量self.settings靈活的訪問和設置插件的屬性,極大減小了代碼的冗余度和耦合度。

3、pygtk中延時器的運用,主要用於需要耗費時間較長的函數中,如設置主界面的透明度或者初始化主界面函數時,通過增加延時使得函數可以完全正確執行。

def __init__( self, applet, iid ):

self.load_timer_id = GObject.timeout_add(50, self.runMainWinLoad)

def runMainWinLoad(self):
GObject.source_remove(self.load_timer_id)

4、try ... except 與 try ... except Exception, e:的應用場景差別

後者包含於前者,後者主要應用於當需要對異常的具體類型進行特殊說明時,"e"代表代碼具體錯誤、“cause”代表引起異常的原因、“detail”代表異常的具體信息等等,如下面的代碼:

def bind_hot_key (self):
try:
if self.hotkeyText != "":
self.keybinder.grab( self.hotkeyText )
self.keybinder.connect("activate", self.onBindingPress)
self.keybinder.start()
# Binding menu to hotkey
print "Binding to Hot Key: " + self.hotkeyText

except Exception, cause:
print "** WARNING ** - Menu Hotkey Binding Error"
print "Error Report :\n", str(cause)
pass
def detect_desktop_environment (self):
self.de = "mate"
try:
de = os.environ["DESKTOP_SESSION"]
if de in ["gnome", "gnome-shell", "mate", "kde", "xfce"]:
self.de = de
else:
if os.path.exists("/usr/bin/caja"):
self.de = "mate"
elif os.path.exists("/usr/bin/thunar"):
self.de = "xfce"
except Exception, detail:
print detail

5、self.button_box.set_homogeneous( False )時什麼意思?

  homogeneous 表示子構件是否具有同樣的大小 True -> 子構件大小相同 False-> 子構件大小根據子構件本身設置屬性決定。

OpenCV官方教程中文版(For Python) PDF http://www.linuxidc.com/Linux/2015-08/121400.htm

Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV無法讀取視頻文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基於QT和OpenCV的人臉識別系統 http://www.linuxidc.com/Linux/2011-11/47806.htm

[翻譯]Ubuntu 14.04, 13.10 下安裝 OpenCV 2.4.9 http://www.linuxidc.com/Linux/2014-12/110045.htm

OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved