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

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

python 安装virtualenv和virtualenvwrapper的方法

1. 首先介绍pip常用命令 pip安装命令: pip install package_name pip升级命令:pip install –ungrage package_name...

python使用socket进行简单网络连接的方法

本文实例讲述了python使用socket进行简单网络连接的方法。分享给大家供大家参考。具体如下: import socket print "Creating socket...",...

python 进程 进程池 进程间通信实现解析

1.python 中创建进程的两种方式: from multiprocessing import Process import time def test_(): print '...

Python读写配置文件的方法

本文实例讲述了Python读写配置文件的方法。分享给大家供大家参考。具体分析如下: python 读写配置文件在实际应用中具有十分强大的功能,在实际的操作中也有相当简捷的操作方案,以下的...

Python使用面向对象方式创建线程实现12306售票系统

目前python 提供了几种多线程实现方式 thread,threading,multithreading ,其中thread模块比较底层,而threading模块是对thread做了一...