使用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. 詳解Android studio 動態fragment的用法2. 編程語言PHP在Web開發領域的優勢在哪?3. 什么是python的自省4. Spring Boot和Thymeleaf整合結合JPA實現分頁效果(實例代碼)5. 解決Android studio xml界面無法預覽問題6. 基于android studio的layout的xml文件的創建方式7. Android如何加載Base64編碼格式圖片8. Springboot Druid 自定義加密數據庫密碼的幾種方案9. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應用實現10. 圖文詳解vue中proto文件的函數調用

網公網安備