文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

python读取文件名及后缀详解

2024-04-02 19:55

关注

因为某些原因,需要读取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的文件都已经筛选出来了

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯