發表文章

[環境設定] 應用程式執行別名

圖片
英文對應:Manage app execution aliases 在Windows使用Microsoft Store下載Python,其實並不會在Program Files等資料夾建立程式檔的資料夾,而是會存到個人的目錄底下,然後在個人的AppData\Local\Microsoft\WindowsApps下建立一系列的程式連結(像是捷徑,但實質概念卻是UNIX的link),這種做法很像我們在UNIX環境下,常常會有一個bin資料夾(不管是/bin或/usr/local/bin或是個人目錄下自建的bin),把所有的程式儲放在這裡,這樣PATH環境變數就不用加一堆路徑了。 但日前不知為何這個程式連結失效,在「命令提示字元」執行python都會出現失效的訊息,雖然在AppData\Local\Microsoft\WindowsApps下的python.exe等都存在,後來上網查資料再自己摸索之後,發現只要使用「設定>應用程式>應用程式與功能> 應用程式執行別名 」,將其開關切換一次即恢復正常,可能在這樣的過程中會重建連結吧!

[執行緒] 使用批次檔來完成並行程序的簡單方法

@REM DOS BATCH @ECHO OFF PYTHON "test1.py" START PYTHON "test2.py" PYTHON "test3.py"

[變數] 使用全域變數

##global variable here ## myvar = 'test1' def my_func1():      ##it will print the content of global  variable ##     print( myvar ) my_func1() def my_func2():     myvar = ' test2 '      ##it will print the content of local  variable ##     print( myvar ) my_func2() ##the content of global  variable does not change ## print( myvar ) def my_func3():      ##use global variable in function like this ##      global  myvar      myvar = ' test3'     print( myvar ) my_func3() ##the content of global  variable will be changed in the function ## print( myvar )

[臨時檔] 開啟臨時檔

##import## import tempfile ##generate  temporary file and write ## with  tempfile.NamedTemporaryFile( suffix='.txt',  prefix='tmp',   dir='d:\\cykuo\\tmp' ,  delete=False) as f:     print(f.name)     f.write( b 'test data')      #若沒有加 b , 會出現 Excption:     #  TypeError: a bytes-like object is required, not 'str'

[臨時檔] 不需實際開臨時檔就取得檔名

  ##import## import tempfile ##get  temporary file name only ## tmp_filename = next(tempfile._get_candidate_names())

[例外處理] 發出例外加訊息

raise  Exception ('ERROR Messages...') #catch and print the exception message try :     (...codes...) except Exception as e:      print(e)