浙江省高等学校教师教育理论培训

微信搜索“教师资格证岗前培训”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Get File Name from File Path in Python | Code Comments

Let’s say you did a search for files matching a certain pattern in a directory using Python:

import glob
 
filePaths = glob.glob("C:\\Temp\\*.txt")
print filePaths

This will list the full file paths with a .txt extension in the C:\Temp directory. For example: C:\\Temp\\test.txt.

But if you wanted to get just the file name, how would you go about that? It took me a little while to find an answer, and the method not super obvious, so I’ll post it here.

import glob, os
 
filePaths = glob.glob("C:\\Temp\\*.txt")
 
for filePath in filePaths:
  print os.path.basename(filePath)
posted on 2012-04-25 13:50  lexus  阅读(1152)  评论(0编辑  收藏  举报