• 【Python】读取文件的名字和文件后缀名



    在这里插入图片描述

    前言


    一、为什么要获取文件后缀?

    有些时候需要获取到文件的类型,然后做一个判断处理。

    二、使用步骤

    1.文本类型的之间判断

    代码如下(示例):
    方法1 这个方法相当于是 通过 . 来split 来获取到的,自己也可以通过split(‘.’)来实现。
    PC端的名字里面是不能包含 "."的特殊符号。

    file_path = '需求.txt'
    filename, file_type = os.path.splitext(file_path)
    print(filename, file_type)
    
    • 1
    • 2
    • 3

    或者你可以直接判断是否包含 ‘.txt’ 字符来判断

    
    print('.txt' in file_path)
    
    • 1
    • 2

    2.第三方库filetype

    pip3 install  filetype
    
    • 1

    代码如下(示例):

    import filetype
    # pip3 install  filetype
    # 需要真实存在的文件才能判断
    #  支持的类型有限
    file_path = r'C:\Users\Administrator\Desktop\getit.png]'
    with open(file_path, 'rb') as f:
        kind = filetype.guess(f)
        print(kind.extension, kind.mime)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    支持的类型如下:

    可以具体查看官网

    Supported types
    Image:
    dwg - image/vnd.dwg
    
    xcf - image/x-xcf
    
    jpg - image/jpeg
    
    jpx - image/jpx
    
    png - image/png
    
    apng - image/apng
    
    gif - image/gif
    
    webp - image/webp
    
    cr2 - image/x-canon-cr2
    
    tif - image/tiff
    
    bmp - image/bmp
    
    jxr - image/vnd.ms-photo
    
    psd - image/vnd.adobe.photoshop
    
    ico - image/x-icon
    
    heic - image/heic
    
    Video:
    3gp - video/3gpp
    
    mp4 - video/mp4
    
    m4v - video/x-m4v
    
    mkv - video/x-matroska
    
    webm - video/webm
    
    mov - video/quicktime
    
    avi - video/x-msvideo
    
    wmv - video/x-ms-wmv
    
    mpg - video/mpeg
    
    flv - video/x-flv
    
    Audio:
    aac - audio/aac
    
    mid - audio/midi
    
    mp3 - audio/mpeg
    
    m4a - audio/mp4
    
    ogg - audio/ogg
    
    flac - audio/x-flac
    
    wav - audio/x-wav
    
    amr - audio/amr
    
    aiff - audio/x-aiff
    
    Archive:
    br - application/x-brotli
    
    rpm - application/x-rpm
    
    dcm - application/dicom
    
    epub - application/epub+zip
    
    zip - application/zip
    
    tar - application/x-tar
    
    rar - application/x-rar-compressed
    
    gz - application/gzip
    
    bz2 - application/x-bzip2
    
    7z - application/x-7z-compressed
    
    xz - application/x-xz
    
    pdf - application/pdf
    
    exe - application/x-msdownload
    
    swf - application/x-shockwave-flash
    
    rtf - application/rtf
    
    eot - application/octet-stream
    
    ps - application/postscript
    
    sqlite - application/x-sqlite3
    
    nes - application/x-nintendo-nes-rom
    
    crx - application/x-google-chrome-extension
    
    cab - application/vnd.ms-cab-compressed
    
    deb - application/x-deb
    
    ar - application/x-unix-archive
    
    Z - application/x-compress
    
    lzo - application/x-lzop
    
    lz - application/x-lzip
    
    lz4 - application/x-lz4
    
    zstd - application/zstd
    
    Font:
    woff - application/font-woff
    
    woff2 - application/font-woff
    
    ttf - application/font-sfnt
    
    otf - application/font-sfnt
    
    Application
    wasm - application/wasm
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140

    总结

    欢迎关注,留言,咨询,交流!
    在这里插入图片描述

  • 相关阅读:
    2023年9月 青少年软件编程等级考试Scratch二级真题
    Matlab 常用快捷键
    使用Spring Boot + MyBatis实现多数据源
    责任链模式
    全球疫情带来的危机与机遇
    【Jmeter】jmeter | windows安装jmeter
    如何使用 Tokenview NFT API进行 NFT 钱包的开发?
    Dubbo基础
    [2022 CISCN]初赛 web题目复现
    外包干了5天,技术退步明显.......
  • 原文地址:https://blog.csdn.net/weixin_43444734/article/details/127550840