os.path.basename()返回path最后的文件名。若path以/或\结尾,则返回空值。 即os.path.split(path)的第二个元素。
>>> import os
>>> path = '/Users/beazley/Data/data.csv'
>>> # Get the last component of the path
>>> os.path.basename(path)
'data.csv'
遍历列表获取文件名:
import os
def print_hi():
file_path = ["/home/xxx/aaa.txt","/home/xxx/bbb.txt"]
for index, item in enumerate(file_path):
print(os.path.basename(item))
if __name__ == '__main__':
print_hi()