python读取文件名及后缀详解

 更新时间:2021年10月26日 08:49:26   作者:番茄土豆牛肉煲  
下面小编就为大家分享一篇关于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的文件都已经筛选出来了

总结

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

相关文章

  • Python+matplotlib实现饼图的绘制

    Python+matplotlib实现饼图的绘制

    Matplotlib是一个Python的2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。本文将利用Matplotlib库绘制饼图,感兴趣的可以了解一下
    2022-03-03
  • python连接mongodb密码认证实例

    python连接mongodb密码认证实例

    今天小编就为大家分享一篇python连接mongodb密码认证实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • Python利用matplotlib.pyplot.boxplot()绘制箱型图实例代码

    Python利用matplotlib.pyplot.boxplot()绘制箱型图实例代码

    相信大家应该都知道Python绘制箱线图主要用matplotlib库里pyplot模块里的boxplot()函数,下面这篇文章主要给大家介绍了关于Python利用matplotlib.pyplot.boxplot()绘制箱型图的相关资料,需要的朋友可以参考下
    2022-08-08
  • python提取字符串中的数字的实现

    python提取字符串中的数字的实现

    本文主要介绍了python提取字符串中的数字的实现,主要介绍了几种常见的方法,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • Python实现快速生成SQL语句的示例详解

    Python实现快速生成SQL语句的示例详解

    这篇文章主要介绍了如何使用Python中的字符串操作和数据结构,以及一些库来自动生成SQL语句的技巧,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-04-04
  • python 定时任务去检测服务器端口是否通的实例

    python 定时任务去检测服务器端口是否通的实例

    今天小编就为大家分享一篇python 定时任务去检测服务器端口是否通的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • 解决python matplotlib imshow无法显示的问题

    解决python matplotlib imshow无法显示的问题

    今天小编就为大家分享一篇解决python matplotlib imshow无法显示的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • python小技巧之批量抓取美女图片

    python小技巧之批量抓取美女图片

    学了python以后,知道python的抓取功能其实是非常强大的,当然不能浪费,呵呵。我平时很喜欢美女图,呵呵,程序员很苦闷的,看看美女,养养眼,增加点乐趣。好,那就用python写一个美女图自动抓取程序吧~~
    2014-06-06
  • wxPython的安装图文教程(Windows)

    wxPython的安装图文教程(Windows)

    下面小编就为大家分享一篇wxPython的安装图文教程(Windows),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • python数据结构之二叉树的建立实例

    python数据结构之二叉树的建立实例

    这篇文章主要介绍了python数据结构之二叉树的建立实例,采用了类似递归方式建立,需要的朋友可以参考下
    2014-04-04

最新评论