在同一循环中交替使用相对路径和绝对路径

Alternate Between Relative and Absolute Path in Same Loop

我正在尝试:

  1. 遍历 CSV 文件目录
  2. 将文件名作为新列附加到每个文件
  3. 将每个文件连接成一个主文件

但是当我将绝对路径转换回相对路径时,我卡在了第 3 步,因为我的输出看起来像 ../../../../Desktop/2018.12.31.csv,而我只是希望它是 2018.12.31

例如,假设该目录包含两个文件:2018.12.31.csv2018.11.30.csv

2018.12.31.csv

A B
1 2

2018.11.30.csv

A B
3 4

在运行之后我的程序:

import os
import pandas as pd

folder = ('/Users/user/Desktop/copy')
files = os.listdir(folder)
file_list = list()

for file in files:

    file = os.path.join(folder, file)
    if file.endswith('.csv'):
        df = pd.read_csv(file, sep=";")
        df['filename'] = os.path.relpath(file)
        file_list.append(df)

all_days = pd.concat(file_list, axis=0, ignore_index=True, sort=False)
all_days.to_csv("/Users/user/Desktop/copy/all.csv")

我希望输出为:

A B filename
1 2 2018.12.31
3 4 2018.11.30

但它是:

A B filename
1 2 ../../../../Desktop/copy/2018.12.31.csv
3 4 ../../../../Desktop/copy/2018.11.30.csv

如果您已经有了 .csv 文件的完整文件路径,您可以使用 os.path 模块只获取文件名:

df['filename'] = os.path.splitext(os.path.split(file)[1])[0]

os.path.splitext() 将路径字符串拆分为一个元组,并将扩展名作为第二个元素。 os.path.split() 将路径字符串拆分为一个元组,文件名(包括扩展名)作为第二个元素。

如果您只使用 .csv 个文件,您可以简化为:

df['filename'] = os.path.split(file)[1][:-4]

os.path.relpath returns 相对于当前目录的文件位置。您可以使用 os.path.basename(path) 获取原始文件名,或者将文件名保留为单独的变量并设置 df['filename'] = file_orig.