Python Glob without the whole path - only the filename

前端 未结 7 2017
死守一世寂寞
死守一世寂寞 2020-12-23 10:47

Is there a way I can use glob on a directory, to get files with a specific extension, but only the filename itself, not the whole path?

7条回答
  •  自闭症患者
    2020-12-23 11:45

    If you are looking for CSV file:

    file = [os.path.basename(x) for x in glob.glob(r'C:\Users\rajat.prakash\Downloads//' + '*.csv')]
    

    If you are looking for EXCEL file:

    file = [os.path.basename(x) for x in glob.glob(r'C:\Users\rajat.prakash\Downloads//' + '*.xlsx')]
    

提交回复
热议问题