如何检查文件的扩展名? [英] How can I check the extension of a file?
本文介绍了如何检查文件的扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在某个程序上工作,根据文件的扩展名,我需要执行不同的操作.我可以用这个吗?
I'm working on a certain program where I need to do different things depending on the extension of the file. Could I just use this?
if m == *.mp3
...
elif m == *.flac
...
推荐答案
假设m
是字符串,则可以使用endswith
:
Assuming m
is a string, you can use endswith
:
if m.endswith('.mp3'):
...
elif m.endswith('.flac'):
...
不区分大小写,并消除了可能很大的else-if链:
To be case-insensitive, and to eliminate a potentially large else-if chain:
m.lower().endswith(('.png', '.jpg', '.jpeg'))
这篇关于如何检查文件的扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文