python读取文件名及后缀详解
目录
- os.listdir()
- 实验代码:
- 输出:
- 结果分析:
- os.path.splitext()
- 实验代码:
- 输出:
- 结果分析:
- os.path.split()
- 实验代码:
- 输出:
- 结果分析:
- 案例分析
- 思路:
- 代码实验:
- 输出:
- 总结
因为某些原因,需要读取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的文件都已经筛选出来了
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注hwidc的更多内容!
【本文由:http://www.1234xp.com/rbzq.html 复制请保留原URL】