获取python文件扩展名和文件名方法

栏目: 编程语言 · Python · 发布时间: 8年前

内容简介:本篇文章通过python写一个获取python文件扩展名和文件名的功能,并分享了代码,有兴趣的参考下。

Python 语言与 Perl,C 和 Java 等语言有许多相似之处,也有一定的差异性,以下是 Python 语言获取文件后缀名和文件名的方法:

#Python获取文件后缀名的方法

import os.path
def file_extension(path):
 return os.path.splitext(path)[1]
print file_extension('/py/a.py')

输出:.py

#Python获取目录和文件名

import os.path
def file_extension(path):
 return os.path.split(path)[1]
print file_extension('/py/a.py')

输出:a.py


以上所述就是小编给大家介绍的《获取python文件扩展名和文件名方法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Understanding Computation

Understanding Computation

Tom Stuart / O'Reilly Media / 2013-6-3 / USD 39.99

Finally, you can learn computation theory and programming language design in an engaging, practical way. Understanding Computation explains theoretical computer science in a context you'll recognize, ......一起来看看 《Understanding Computation》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具