1. 程式人生 > >os.path.dirname用法

os.path.dirname用法

目錄結構

我的目錄結構:
目錄結構

os.path.dirname(path)

語法:os.path.dirname(path)
功能:去掉檔名,返回目錄
如:

print(os.path.dirname("E:/Read_File/read_yaml.py"))
#結果:
E:/Read_File
print(os.path.dirname("E:/Read_File"))
#結果:
E:/

os.path.dirname(__file__)

先了解一下__file__

print(__file__)
#結果
E:/Read_File/read_yaml.py

可以看出__file__表示了當前檔案的path

那麼就可以瞭解到os.path.dirname((__file__)和os.path.dirname(“E:/Read_File/read_yaml.py”)是一個意思
再根據os.path.dirname(path)的用法,得出os.path.dirname((__file__)就是得到當前檔案的絕對路徑

print(os.path.dirname(__file__))
#結果:
E:/Read_File

擴充套件

若print os.path.dirname(file)所在指令碼是以絕對路徑執行的,則會輸出該指令碼所在的絕對路徑,若以相對路徑執行,輸出空目錄

print
(os.path.dirname(__file__))

結果:
結果

相關推薦

os.path.dirname用法

目錄結構 我的目錄結構: os.path.dirname(path) 語法:os.path.dirname(path) 功能:去掉檔名,返回目錄 如: print(os.path.dirname("E:/Read_File/read_ya

Python模塊詳解以及import本質,獲得文件當前路徑os.path.abspath,獲得文件的父目錄os.path.dirname,放到系統變量的第一位sys.path.insert(0,x)

alt 獲取 詳解 nbsp spa 絕對路徑 解釋 系統 port 模塊介紹 1、定義: 模塊:用來從邏輯上組織python代碼(變量,函數,類,邏輯:實現一個功能),本質就是.py結尾的python文件(文件名:test.py,對應的模塊名:test) 包:用來從邏輯上

Python中join函數和os.path.join用法

post () 參數說明 python class log join 絕對路徑 返回 Python中有join和os.path.join()兩個函數,具體作用如下: join:連接字符串數組。將字符串、元組、列表中的元素以指定的字符(分隔符)連接生成一個新的字符串 os.p

os.path.dirname( __ file __ ) 2018/6/2

finish os.path 命令 IT import ror 文件的 解釋 進行 os.path.dirname( __ file __ ) 2018/6/2 該測試腳本所在的位置:D:\第1層\第2層\第3層\第4層\第5層\test11.py import os #該

Python3基礎 os.path.dirname 對路徑字符串進行處理 返回所在文件夾的路徑

charm finish 進行 art rate set rules dem 操作 ? python : 3.7.0 OS : Ubuntu 18.04.1 LTS

os.getcwd()與os.path.dirname(__file__)

在學習python的os模組時,遇到了兩種獲得當前目錄的方法: 1.os.getcwd() 2.os.path.dirname(file) 下面探索一下他們的區別: 1.在F:\AI\Allchapter\pythonscientic\chapter05 中建立fileComment.py

.os.path.abspath(path)、os.path.dirname(path)、os.path.basename(path)等等關於python os.path模組常用方法詳解

裡面包含.os.path.abspath(path)、os.path.split(path)、os.path.dirname(path)、os.path.basename(path)、os.path.commonprefix(list)、os.path.exists(path)、os.path.is

os.path.splitext()用法--分離檔名與副檔名

用法: os.path.splitext(“檔案路徑”)    分離檔名與副檔名;預設返回(fname,fextension)元組,可做分片操作 例子: import os path_01='E:\STH\Foobar2000\install.log'

Python3 join函式和os.path.join用法

Python3  join函式和os.path.join用法 os.path.join()連線兩個檔名地址的時候,就比os.path.join("D:\","test.txt")結果是D:\test.txt # Python3 # author:lim # date:2019-01-14

python中os.path.dirname(__file__)和sys.path[0]使用

python中os.path.dirname(__file__)的使用  (1).當"print os.path.dirname(__file__)"所在指令碼是以完整路徑被執行的, 那麼將輸出該指令碼所在的完整路徑,比如:       python c:/python2

os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用

import os def file_path(): '''開啟當前執行指令碼的絕對路徑''' paths=os.path.dirname(__file__) print pa

python零碎知識os.path.dirname(__file__)

import os.path as osp print osp.dirname(__file__)如果直接在python console 中或者命令列中執行上面程式碼,則會報如下錯誤:NameError: name '__file__' is not defined 原因是

os.path.join 的用法

get 元素 import 返回值 連接 data padding 自動 http Python中有join和os.path.join()兩個函數,具體作用如下: join:連接字符串數組。將字符串、元組、列表中的元素以指定的字符(分隔符)連接生成一個新的字符串os.pat

python中os.path.isdir()和os.path.isfile()的正確用法

之前網上查詢os.path.isdir()和os.path.isfile()的使用;發現很多是錯誤的,主要原因是,傳入的引數不是絕對路徑。 先介紹一下os.listdir()方法,此方法返回一個列表,其中包含有指定路徑下的目錄和檔案的名稱 import os dirct = '/home/w

python3中,os.path模組下常用的用法總結

第一部分 python3中,os.path模組下常用的用法總結 abspath 返回一個目錄的絕對路徑 Return an absolute path. >>> os.path.abspath("/etc/sysconfig/selinux") '/e

os.path和sys.path的相關用法

1.os.path (1) os.path.abspath(path) 功能:返回path的絕對路徑 如: print os.path.abspath('test.py') #結果 /home/username/mytest/test.py (2) os.path.dirn

python中os.path.isdir()等函數的作用和用法

需要 () pri pre pytho lis tdi port txt   一 用法和概念:   Python中的os模塊用於和系統進行交互,其中:    1 os.listdir()用於返回一個由文件名和目錄名組成的列表,需要註意的是它接收的參數需要是一個絕對的路徑

python基礎(一)--join和os.path.join函式的用法以及字串格式化

join:連線字串陣列。將字串、元組、列表中的元素按指定的字元(分割符)連線成一個新的字串。 os.path.join:將多個路徑組合並返回。 Python格式化字串 1、格式化操作符(%) 格式化符號說明 %c轉換成字元(ASCII碼值,或者長度為一的字串) %r優先用

詳解Python中的join()函式的用法(字串和os.path

函式:string.join() Python中有join()和os.path.join()兩個函式,具體作用如下: join(): 連線字串陣列。將字串、元組、列表中的元素以指定的字元(分隔符)連線生成一個新的字串 os.path.

python路徑拼接os.path.join()函式用法和作用

os.path.join()函式用於路徑拼接檔案路徑。 os.path.join()函式中可以傳入多個路徑: 會從第一個以”/”開頭的引數開始拼接,之前的引數全部丟棄。 以上一種情況為先。在上一種情況確保情況下,若出現”./”開頭的引數,會從”./”開頭的引數