python OS(一)獲取絕對路徑

2022-01-21 23:09:23 字數 1294 閱讀 7204

python常用模組(資料夾)

python_os(資料夾)

os_獲取絕對路徑.py

### 方法一

os_獲取絕對路徑.py

#coding:utf8

import os

#獲取當前目錄絕對路徑

dir_path = os.path.dirname(os.path.abspath(__file__))

print('當前目錄絕對路徑:',dir_path)

#獲取上級目錄絕對路徑

dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

print('上級目錄絕對路徑:',dir_path)

執行結果
當前目錄絕對路徑: d:\python常用模組\python_os

上級目錄絕對路徑: d:\python常用模組

簡化**

# coding :utf8

from os.path import *

# 獲取當前目錄絕對路徑

dir_path = dirname(abspath(__file__))

print('當前目錄絕對路徑:', dir_path)

# 獲取上級目錄絕對路徑

dir_path = dirname(dirname(abspath(__file__)))

print('上級目錄絕對路徑:', dir_path)h)

執行結果
當前目錄絕對路徑: d:\python常用模組\python_os

上級目錄絕對路徑: d:\python常用模組

os_獲取絕對路徑.py

import os

#獲取當前目錄絕對路徑

dir_path = os.path.abspath(os.path.split(__file__)[0])

print('當前目錄絕對路徑:',dir_path)

#獲取上級目錄絕對路徑

dir_path = os.path.abspath(os.path.split(os.path.split(__file__)[0])[0])

print('上級目錄絕對路徑:',dir_path)

執行結果
當前目錄絕對路徑: d:\python常用模組\python_os

上級目錄絕對路徑: d:\python常用模組

jeecms獲取絕對路徑

在jeecms後台管理做乙個附件上傳到伺服器上,然後讀取改上傳檔案,半天獲取不到路徑,後來發現有定義好的絕對路徑獲取方法 最好將檔案上傳到u資料夾底下 string path u cms www 201807 26111223imd7.xls file file new file realpathr...

檔案絕對路徑獲取

yourclass.class.getclassloader getresource threadpoolconfig.xml getpath 通過上述方法獲取到的一般是指定檔案的絕對路徑 並且該檔案必須存放在src目錄下 存在的問題 如果絕對路徑中存在空格,getpath取到的值是經過urlenc...

絕對路徑 相對路徑的獲取

呼叫函式 getmodulefilename,返回當前程序的映象檔案 exe 所在的目錄。較為穩妥且移植性較好。函式原型 函式功能 此函式得到當前應用程式的執行目錄,還包括應用程式的檔名。引數 hmodule handle to module 要獲取檔名的模組控制代碼。null表示當前模組 lpfi...