Home >Backend Development >Python Tutorial >python根据给定文件返回文件名和扩展名的方法

python根据给定文件返回文件名和扩展名的方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-10 15:16:521504browse

本文实例讲述了python根据给定文件返回文件名和扩展名的方法。分享给大家供大家参考。具体分析如下:

这段代码可以根据文件的完整路径返回文件名和扩展名,python的函数可以同时返回两个值,用起来就更方便了

def GetFileNameAndExt(filename):
 import os
 (filepath,tempfilename) = os.path.split(filename);
 (shotname,extension) = os.path.splitext(tempfilename);
 return shotname,extension

测试代码

print(GetFileNameAndExt('c:\jb51\index.html'))

返回结果:('index', '.html')

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn