使用Python獲取當(dāng)前工作目錄和執(zhí)行命令的位置
獲取當(dāng)前工作目錄
import sys
print(sys.path[0])
獲取執(zhí)行命令的位置
import os
print(os.getcwd())
補(bǔ)充知識:Python獲取當(dāng)前執(zhí)行文件,根據(jù)某一級目錄名稱,獲取此目錄名稱所在的絕對路徑
假如當(dāng)前文件絕對路徑:E:learnpython我的filemy.py
#coding:utf-8import os #dirName:上級目錄名稱#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í)行命令的位置就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python如何換行輸出2. Java 接口和抽象類的區(qū)別詳解3. Android Studio中一套代碼多渠道打包的實(shí)現(xiàn)方法4. python如何計(jì)算圓的面積5. Java使用Tesseract-Ocr識別數(shù)字6. Python:UserWarning:此模式具有匹配組。要實(shí)際獲得組,請使用str.extract7. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼8. Android打包篇:Android Studio將代碼打包成jar包教程9. 詳解java google Thumbnails 圖片處理10. 解決Android Studio 格式化 Format代碼快捷鍵問題
