使用Python獲取當(dāng)前工作目錄和執(zhí)行命令的位置
獲取當(dāng)前工作目錄
import sys
print(sys.path[0])
獲取執(zhí)行命令的位置
import os
print(os.getcwd())
補(bǔ)充知識(shí):Python獲取當(dāng)前執(zhí)行文件,根據(jù)某一級(jí)目錄名稱(chēng),獲取此目錄名稱(chēng)所在的絕對(duì)路徑
假如當(dāng)前文件絕對(duì)路徑:E:learnpython我的filemy.py
#coding:utf-8import os #dirName:上級(jí)目錄名稱(chēng)#sysCoding:系統(tǒng)編碼格式#targetCoding:轉(zhuǎn)換目標(biāo)編碼格式def get_dir_realpath(dirName,sysCoding,targetCoding): path = os.path.split(os.path.realpath(__file__))[0].decode(sysCoding).encode(targetCoding) dirList = path.split('') length = len(dirList) for _ in range(1,length): fileName = os.path.split(path)[1] path = os.path.split(path)[0] if fileName == dirName: return path break return '' print get_dir_realpath('我的file',’cp936’,'utf-8') 執(zhí)行結(jié)果:E:learnpython
print get_dir_realpath('python',’cp936’,'utf-8') 執(zhí)行結(jié)果:E:learn
以上這篇使用Python獲取當(dāng)前工作目錄和執(zhí)行命令的位置就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. chat.asp聊天程序的編寫(xiě)方法3. CSS 使用Sprites技術(shù)實(shí)現(xiàn)圓角效果4. phpstudy apache開(kāi)啟ssi使用詳解5. 詳解瀏覽器的緩存機(jī)制6. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法7. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?8. HTML中的XML數(shù)據(jù)島記錄編輯與添加9. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法10. 推薦一個(gè)好看Table表格的css樣式代碼詳解
