How to extract the file name from a file path?

后端 未结 4 382
我在风中等你
我在风中等你 2021-01-04 03:51

I have the following code:

os.listdir(\"staging\")

# Seperate filename from extension
sep = os.sep

# Change the casing
for n in os.listdir(\"staging\"):
           


        
4条回答
  •  旧时难觅i
    2021-01-04 04:48

    This article here worked out just fine for me

    import os
    inputFilepath = 'path/to/file/foobar.txt'
    filename_w_ext = os.path.basename(inputFilepath)
    filename, file_extension = os.path.splitext(filename_w_ext)
    #filename = foobar
    #file_extension = .txt
    
    path, filename = os.path.split(path/to/file/foobar.txt)
    # path = path/to/file
    # filename = foobar.txt
    

    Hope it helps someone searching for this answer

提交回复
热议问题