Python 獲取檔案目錄

2021-10-19 12:44:30 字數 472 閱讀 8481

cwd = os.getcwd(

)# 獲取檔案路徑

print

(cwd)

print

(os.path.basename(cwd))#

print

(os.path.dirname(cwd)

)

# basename返回path最後的檔名。如果path以/或\結尾,那麼就會返回空值。

os.path.basename(

'c:\test.csv'

)'test.csv'

#(這裡csv被當作檔名處理了) csv是乙個目錄

os.path.basename(

'c:\csv'

)'csv'

參考:

os.path.basename()方法

python如何獲取不含路徑的當前資料夾的名稱

python 獲取當前目錄下的檔案目錄和檔名

os模組下有兩個函式 os.walk os.listdir 1 coding utf 8 23 import os 45 deffile name file dir 6for root,dirs,files in os.walk file dir 7print root 當前目錄路徑 8print ...

Python 獲取當前檔案所在目錄

python下獲取檔案所在的絕對目錄,大都通過 os.path.abspath,但如果你在其他目錄下,通過絕對路徑獲取,這時就會有錯。例如 步驟 在當用目錄執行 import os import sys print os.path.abspath main.py 結果為 home abc tiran...

獲取目錄檔案數

csdn相關解釋 int count int cgetfilecountdlg getfilecount cstring szpath void cgetfilecountdlg recurse lpctstr pstr if it s a directory,recursively search ...