获取基本文件名

1
2
filename="/tmp/test.txt"
os.path.basename(filename)

获取基本文件名(不包括拓展名)

1
2
filename="/tmp/test.txt"
os.path.splitext(os.path.basename(filename))[0]

获取文件拓展名

1
2
filename="/tmp/test.txt"
os.path.splitext(os.path.basename(filename))[1]

获取父目录

1
2
filename="/tmp/test.txt"
os.path.dirname(filename)

参考

  1. http://stackoverflow.com/questions/678236/how-to-get-the-filename-without-the-extension-from-a-path-in-python

留言