string - How to get the filename without the extension from a path in Python? - Stack Overflow

string - How to get the filename without the extension from a path in Python? - Stack Overflow

string - How to get the filename without the extension from a path in Python? - Stack Overflow

up vote
32
down vote
accepted

Getting the name of the file without the extension :

import os
print os.path.splitext("path_to_file")[0]

As for your import problem, you solve it this way :

from os.path import basename

# now you can call it directly with basename
print basename("/a/b/c.txt")
posted on 2012-06-05 09:08  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/06/05/2536026.html

你可能感兴趣的:(string - How to get the filename without the extension from a path in Python? - Stack Overflow)