使用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í)目錄名稱,獲取此目錄名稱所在的絕對(duì)路徑
假如當(dāng)前文件絕對(duì)路徑:E:learnpython我的filemy.py
#coding:utf-8import os #dirName:上級(jí)目錄名稱#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(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明2. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法3. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析4. PHP設(shè)計(jì)模式中工廠模式深入詳解5. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)9. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)10. CSS hack用法案例詳解
