在Python中,我们可以使用os模块的path.splitext()函数来判断文件的后缀,这个函数会将文件名和扩展名分开,返回一个元组,第一个元素是文件名,第二个元素是扩展名。,步骤如下:,1、导入os模块。,2、使用os.path.splitext()函数分割文件名和扩展名。,3、打印或返回扩展名。,代码如下:,在这个例子中,我们定义了一个函数get_file_extension(),它接受一个参数filename,然后使用os.path.splitext()函数来分割文件名和扩展名,并返回扩展名。, ,import os def get_file_extension(filename): # 使用os.path.splitext()函数分割文件名和扩展名 file_name, extension = os.path.splitext(filename) return extension 测试 print(get_file_extension(‘example.txt’)) # 输出: .txt,
原创文章,作者:admin,如若转载,请注明出处:https://www.vaicdn.com/news/73036.html