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

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

WBOY
Release: 2016-06-10 15:16:52
Original
1481 people have browsed it

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

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

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

测试代码

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

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template