續前文[日常]Beyond的歌里最多是"唏噓"嗎? - Python分詞+詞頻最后的想法, 發現VS Code支持用戶自定義代碼片段: Creating your own snippets in Visual Studio Code
嘗試如下, 片段使用中文命名, 但前綴好像不能用中文(如果前綴設定為"檔案", 在原始碼編輯時輸入"檔案"后沒有自動補全彈窗), 應該與自動補全機制有關.

片段定義:
{
"讀檔案": {
"prefix": "file",
"body": [
"with open(${1:檔案全路徑}) as 檔案:",
"\t內容 = 檔案.read()"
],
"description": "讀取某檔案"
},
"讀所有檔案": {
"prefix": "file",
"body": [
"import os",
"路徑 = '.'",
"for 檔案名 in os.listdir(路徑):",
"\twith open(os.path.join(路徑, 檔案名)) as 檔案:",
"\t\t內容 = 檔案.read()"
],
"description": "讀取某目錄下所有檔案"
}
}
下一步把這些片段打包成擴展: Snippet Guide
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/215417.html
標籤:其他
