如何在结果中只列出目录中没有目录信息的文件名?我试过了
for file in glob.glob(dir+filetype):
print file
给我结果
/path_name/1.log,/path_name/2.log,....
但我只需要文件名:1.log
、2.log
等。我不需要结果中的目录信息。有没有简单的方法来摆脱路径信息?我不想对结果进行一些修改。谢谢!
最佳答案
Return the base name of pathname
path
. This is the second element of the pair returned by passingpath
to the functionsplit()
. Note that the result of this function is different from the Unixbasename
program; wherebasename
for '/foo/bar/' returns 'bar', thebasename()
function returns an empty string ('').
所以:
>>> os.path.basename('/path_name/1.log,/path_name/2.log')
'2.log'
关于python - 如何获取没有目录名的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16090670/