How to get the filename without the extension from a path in Python?

前端 未结 23 1870
逝去的感伤
逝去的感伤 2020-11-22 05:43

How to get the filename without the extension from a path in Python?

For instance, if I had "/path/to/some/file.txt", I would want "

23条回答
  •  余生分开走
    2020-11-22 06:26

    import os
    list = []
    def getFileName( path ):
    for file in os.listdir(path):
        #print file
        try:
            base=os.path.basename(file)
            splitbase=os.path.splitext(base)
            ext = os.path.splitext(base)[1]
            if(ext):
                list.append(base)
            else:
                newpath = path+"/"+file
                #print path
                getFileName(newpath)
        except:
            pass
    return list
    
    getFileName("/home/weexcel-java3/Desktop/backup")
    print list
    

提交回复
热议问题