Python-合并多个文件然后删除源文件时出错

Python-合并多个文件然后删除源文件时出错,python,python-3.x,merge,shutil,Python,Python 3.x,Merge,Shutil,我正在尝试将文件夹中的一组文件合并到一个文件中,然后删除合并文件之外的所有文件 我犯了一个错误 shutil.SameFileError: '/Users/user/folder/.DS_Store' and '/Users/user/folder/.DS_Store' are the same file 下面是我正在使用的代码: base=文件提取到的路径 base2=基本文件夹中的子文件夹 allFiles = glob.glob(base2 + "/*") list_ = []

我正在尝试将文件夹中的一组文件合并到一个文件中,然后删除合并文件之外的所有文件

我犯了一个错误

shutil.SameFileError: '/Users/user/folder/.DS_Store' and '/Users/user/folder/.DS_Store' are the same file
下面是我正在使用的代码:

base=文件提取到的路径 base2=基本文件夹中的子文件夹

allFiles = glob.glob(base2 + "/*")
    list_ = []
    for file_ in allFiles:
        try:
            df = pd.read_csv(file_, index_col=None, header=None, sep = ',', engine = 'python')
            list_.append(df)
        except pd.errors.EmptyDataError:
            continue
    # Deleting all files except the concatenated file by day
    os.system("cd /{}/{}/{}/{};find . -maxdepth 1 -type f -size 0 -delete".format(base,
            file_type, parent, child))
    updatedfiles = glob.glob(base2 + "/*")
    try:
        df_v1 = [pd.read_csv(fp, sep=',').assign(FileName=os.path.basename(fp)) for fp in updatedfiles]
        df = pd.concat(df_v1, ignore_index=True, sort=False)
        df.to_csv(base2 + "/{}_{}.csv".format(date, parent), index=False)
    except Exception as e:

    # Merging final output file
    file_from = "{}/{}/{}".format(base, parent, child)
    file_to = "{}/{}/{}".format(base, parent, child)
    for root, dirs, files in os.walk((os.path.normpath(file_from)), topdown=False):
        for name in files:
            SourceFolder = os.path.join(root, name)
            shutil.copy2(SourceFolder, file_to) <<-- Error is thrown at this line
    # Deleting any sub-folders within the folder
    if os.path.exists(filename) and not os.path.isdir(filename) and not os.path.islink(filename):
        os.remove(filename)
    updatedfiles_1 = glob.glob(file_from + "/*")
    try:
        df_v0 = pd.concat([pd.read_csv(f) for f in updatedfiles_1], sort=True)
        df_v0.to_csv('{}/{}_{}'.format(final_files, parent, child), index=False)
    except Exception as e:
        df_v0.to_csv('{}/{}_{}'.format(final_files, parent, child), index=False)
allFiles=glob.glob(base2+“/*”)
列表\=[]
对于所有文件中的文件:
尝试:
df=pd.read\u csv(文件、索引、列=None、头=None、sep='、',引擎='python')
列表附加(df)
除pd.errors.EmptyDataError外:
持续
#按天删除除连接文件以外的所有文件
system(“cd/{}/{}/{}/{};find.-maxdepth 1-type f-size 0-delete”。格式(基本,
文件(类型、父级、子级)
updatedfiles=glob.glob(base2+“/*”)
尝试:
df_v1=[pd.read_csv(fp,sep=',')。为更新文件中的fp分配(FileName=os.path.basename(fp))
df=pd.concat(df_v1,ignore_index=True,sort=False)
df.to_csv(base2+“/{}{}{}.csv.”格式(日期,父级),索引=False)
例外情况除外,如e:
#合并最终输出文件
文件_from=“{}/{}/{}”。格式(基、父、子)
文件_to=“{}/{}/{}”。格式(基、父、子)
对于os.walk((os.path.normpath(file_from))中的root、dirs和文件,自上而下=False):
对于文件中的名称:
SourceFolder=os.path.join(根目录,名称)

copy2(SourceFolder,file_to)由于@Mad Lee提到SourceFolder和file_to是相同的,您需要检查两个文件是否不相同。 将此行放在引发错误的行之前

如果是SourceFolder=提交至:
copy2(SourceFolder,file\u to)

在您的代码中,
file\u from
file\u to
是相同的。