使用Python獲取當前工作目錄和執行命令的位置
獲取當前工作目錄
import sys
print(sys.path[0])
獲取執行命令的位置
import os
print(os.getcwd())
補充知識:Python獲取當前執行文件,根據某一級目錄名稱,獲取此目錄名稱所在的絕對路徑
假如當前文件絕對路徑:E:learnpython我的filemy.py
#coding:utf-8import os #dirName:上級目錄名稱#sysCoding:系統編碼格式#targetCoding:轉換目標編碼格式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') 執行結果:E:learnpython
print get_dir_realpath('python',’cp936’,'utf-8') 執行結果:E:learn
以上這篇使用Python獲取當前工作目錄和執行命令的位置就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
1. 基于javaweb+jsp實現企業車輛管理系統2. 怎樣才能用js生成xmldom對象,并且在firefox中也實現xml數據島?3. 利用ajax+php實現商品價格計算4. ASP.Net MVC利用NPOI導入導出Excel的示例代碼5. jstl 字符串處理函數6. JSP動態網頁開發原理詳解7. PHP中為什么使用file_get_contents("php://input")接收微信通知8. .Net core Blazor+自定義日志提供器實現實時日志查看器的原理解析9. IOS蘋果AppStore內購付款的服務器端php驗證方法(使用thinkphp)10. XML CDATA是什么?
