最近在寫一個檔案自動搬移程式,不過因為只要針對PDF做處理,所以看看有沒有套件可以使用,不負眾望Python果然有類似套件,以下說明如何使用
安裝filetype
1 |
pip install filetype |
使用
1 2 3 4 5 |
fileURL = ('file放置位置') kind = filetype.guess(fileURL) #抽出File類型 if kind != None: #判斷是否是可以知道類型的文件 if kind.mime == 'application/pdf': print('This is a PDF File') |
filetype支援的檔案類型
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 |
圖片 • jpg – image/jpeg • png – image/png • 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 視訊檔 • 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 音樂 • mid – audio/midi • mp3 – audio/mpeg • m4a – audio/m4a • ogg – audio/ogg • flac – audio/x-flac • wav – audio/x-wav • amr – audio/amr 電子書/壓縮檔/執行檔/資料庫/文件 • 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 • lz – application/x-lzip 字型 • woff – application/font-woff • woff2 – application/font-woff • ttf – application/font-sfnt • otf – application/font-sfnt |
Ref: