python讀取文件名及後綴詳解

因為某些原因,需要讀取d.lib後綴的文件,在此寫下記錄

os.listdir()

語法:os.listdir(path)

函數用途:輸出路徑下所有文件的文件名

實驗代碼:

假如我們需要讀取該文件夾所包含全部文件名

import os
path = "E:/vs_code/qtVideo/lib"
datanames = os.listdir(path)
for i in datanames:
    print(i)

輸出:

A_TEST
OpenCVConfig-version.cmake
OpenCVConfig.cmake
OpenCVModules-debug.cmake
OpenCVModules-release.cmake
OpenCVModules.cmake
opencv_aruco330.lib
opencv_aruco330d.lib
opencv_bgsegm330.lib
opencv_bgsegm330d.lib
opencv_bioinspired330.lib
opencv_bioinspired330d.lib
opencv_calib3d330.lib
opencv_calib3d330d.lib
opencv_ccalib330.lib
opencv_ccalib330d.lib
opencv_core330.lib
opencv_core330d.lib
opencv_datasets330.lib
opencv_datasets330d.lib
opencv_dnn330.lib
opencv_dnn330d.lib
opencv_dpm330.lib
opencv_dpm330d.lib
opencv_face330.lib
opencv_face330d.lib
opencv_features2d330.lib
opencv_features2d330d.lib
opencv_flann330.lib
opencv_flann330d.lib
opencv_fuzzy330.lib
opencv_fuzzy330d.lib
opencv_highgui330.lib
opencv_highgui330d.lib
opencv_imgcodecs330.lib
opencv_imgcodecs330d.lib
opencv_imgproc330.lib
opencv_imgproc330d.lib
opencv_img_hash330.lib
opencv_img_hash330d.lib
opencv_line_descriptor330.lib
opencv_line_descriptor330d.lib
opencv_ml330.lib
opencv_ml330d.lib
opencv_objdetect330.lib
opencv_objdetect330d.lib
opencv_optflow330.lib
opencv_optflow330d.lib
opencv_phase_unwrapping330.lib
opencv_phase_unwrapping330d.lib
opencv_photo330.lib
opencv_photo330d.lib
opencv_plot330.lib
opencv_plot330d.lib
opencv_reg330.lib
opencv_reg330d.lib
opencv_rgbd330.lib
opencv_rgbd330d.lib
opencv_saliency330.lib
opencv_saliency330d.lib
opencv_shape330.lib
opencv_shape330d.lib
opencv_stereo330.lib
opencv_stereo330d.lib
opencv_stitching330.lib
opencv_stitching330d.lib
opencv_structured_light330.lib
opencv_structured_light330d.lib
opencv_superres330.lib
opencv_superres330d.lib
opencv_surface_matching330.lib
opencv_surface_matching330d.lib
opencv_text330.lib
opencv_text330d.lib
opencv_tracking330.lib
opencv_tracking330d.lib
opencv_video330.lib
opencv_video330d.lib
opencv_videoio330.lib
opencv_videoio330d.lib
opencv_videostab330.lib
opencv_videostab330d.lib
opencv_xfeatures2d330.lib
opencv_xfeatures2d330d.lib
opencv_ximgproc330.lib
opencv_ximgproc330d.lib
opencv_xobjdetect330.lib
opencv_xobjdetect330d.lib
opencv_xphoto330.lib
opencv_xphoto330d.lib

結果分析:

os.listdir  以列表的形式返回指定文件夾的下所有內容,不管是文件還是文件夾

os.path.splitext()

語法:os.path.splitext(文件名)

函數用途:將文件名和擴展名分開。

實驗代碼:

name1 = "opencv_xphoto330d.lib"
name2 = "D:\\opencv_xphoto330d.lib"
result1 = os.path.splitext(name1)
result2 = os.path.splitext(name2)
print("result1:",result1)
print("result2:",result2)
print("type:",type(result1),type(result2))

輸出:

result1: ('opencv_xphoto330d', '.lib')
result2: ('D:\\opencv_xphoto330d', '.lib')
type: <class 'tuple'> <class 'tuple'>

結果分析:

就是以文件名中的’.’作為分隔符,分隔文件名稱與文件後綴,以元組的形式返回文件名與後綴。

os.path.split()

語法:os.path.split(path)

函數用途:返回文件的路徑和文件名。

實驗代碼:

name1 = "opencv_xphoto330d.lib"
name2 = "E:\\vs_code\\qtVideo\\opencv_xphoto330d.lib"
name3 = "E:\\vs_code\\qtVideo"
result1 = os.path.split(name1)
result2 = os.path.split(name2)
result3 = os.path.split(name3)
print("result1:",result1)
print("result2:",result2)
print("result3:",result3)
print("type:",type(result1),type(result2),type(result3))

輸出:

result1: ('', 'opencv_xphoto330d.lib')
result2: ('E:\\vs_code\\qtVideo', 'opencv_xphoto330d.lib')
result3: ('E:\\vs_code', 'qtVideo')
type: <class 'tuple'> <class 'tuple'>

結果分析:

就是以路徑最後一個’/’或者’\\’為分隔,以元祖的形式返回路徑與文件名,若隻有文件名,則輸出文件名與空。

案例分析

指定文件夾下包含.lib、d.lib、其他文件夾,需要獲得文件夾中所有d.lib後綴的文件名

思路:

通過listdir()獲得該文件夾下所有文件名,使用splitext()分離文件名和後綴,判斷文件名最後一個是否為d,後綴是否為lib

代碼實驗:

import os
path = "E:/vs_code/qtVideo/lib"
datanames = os.listdir(path)
for i in datanames:
    if os.path.splitext(i)[1]=='.lib' and  os.path.splitext(i)[0][-1]=='d':
        print(i)

輸出:

opencv_aruco330d.lib
opencv_bgsegm330d.lib
opencv_bioinspired330d.lib
opencv_calib3d330d.lib
opencv_ccalib330d.lib
opencv_core330d.lib
opencv_datasets330d.lib
opencv_dnn330d.lib
opencv_dpm330d.lib
opencv_face330d.lib
opencv_features2d330d.lib
opencv_flann330d.lib
opencv_fuzzy330d.lib
opencv_highgui330d.lib
opencv_imgcodecs330d.lib
opencv_imgproc330d.lib
opencv_img_hash330d.lib
opencv_line_descriptor330d.lib
opencv_ml330d.lib
opencv_objdetect330d.lib
opencv_optflow330d.lib
opencv_phase_unwrapping330d.lib
opencv_photo330d.lib
opencv_plot330d.lib
opencv_reg330d.lib
opencv_rgbd330d.lib
opencv_saliency330d.lib
opencv_shape330d.lib
opencv_stereo330d.lib
opencv_stitching330d.lib
opencv_structured_light330d.lib
opencv_superres330d.lib
opencv_surface_matching330d.lib
opencv_text330d.lib
opencv_tracking330d.lib
opencv_video330d.lib
opencv_videoio330d.lib
opencv_videostab330d.lib
opencv_xfeatures2d330d.lib
opencv_ximgproc330d.lib
opencv_xobjdetect330d.lib
opencv_xphoto330d.lib

可以看到我們把文件中的d.lib的文件都已經篩選出來瞭

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!

推薦閱讀: