How to extract the file name from a file path?

后端 未结 4 357
我在风中等你
我在风中等你 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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 04:26

    If all you want to do is truncate the file paths to just the filename, you can use os.path.basename:

    for file in files:
        fname = os.path.basename(file)
        dict_[fname] = (pd.read_csv(file, header=0, dtype=str, encoding='cp1252')
                          .fillna(''))
    

    Example:

    os.path.basename('Desktop/test.txt')
    # 'test.txt'
    

提交回复
热议问题