【问题标题】:reading file names from a list and then appending them does not append files从列表中读取文件名然后附加它们不会附加文件
【发布时间】:2018-12-30 09:01:20
【问题描述】:

我有一个包含文件名的列表。

我想将所有文件的内容附加到第一个文件中,然后将该文件(附加的第一个文件)复制到新路径。

这是我到目前为止所做的: 这是附加代码的一部分(我在我的问题末尾放了一个可重现的程序,请看一下:)。

if (len(appended) == 1):
    shutil.copy(os.path.join(path, appended[0]), out_path_tempappendedfiles)
else:

    with open(appended[0],'a+') as myappendedfile:
        for file in appended:
                myappendedfile.write(file)
    shutil.copy(os.path.join(path, myappendedfile.name), out_path_tempappendedfiles)

这个将成功运行并成功复制,但它不会附加文件,它只是保留第一个文件的内容。

我也试过这个link 它没有引发错误但没有附加文件。所以除了使用write之外,我使用了相同的代码shutil.copyobject

with open(file,'rb') as fd:
shutil.copyfileobj(fd, myappendedfile)

同样的事情发生了。

更新1 这是整个代码:

即使有更新,它仍然不会追加:

import os

import pandas as pd
d = {'Clinic Number':[1,1,1,2,2,3],'date':['2015-05-05','2015-05-05','2015-05-05','2015-05-05','2016-05-05','2017-05-05'],'file':['1a.txt','1b.txt','1c.txt','2.txt','4.txt','5.txt']}
df = pd.DataFrame(data=d)
df.sort_values(['Clinic Number', 'date'], inplace=True)
df['row_number'] = (df.date.ne(df.date.shift()) | df['Clinic Number'].ne(df['Clinic Number'].shift())).cumsum()

import shutil
path= 'C:/Users/sari/Documents/fldr'
out_path_tempappendedfiles='C:/Users/sari/Documents/fldr/temp'

for rownumber in df['row_number'].unique():
    appended = df[df['row_number']==rownumber]['file'].tolist()
    if (len(appended) == 1):
        shutil.copy(os.path.join(path, appended[0]), out_path_tempappendedfiles)
    else:
        with open(appended[0],'a') as myappendedfile:
            for file in appended:
                fd=open(file,'r')
                myappendedfile.write('\n'+fd.read())
                fd.close()

        Shutil.copy(os.path.join(path, myappendedfile.name), out_path_tempappendedfiles)

请告诉我问题出在哪里?

【问题讨论】:

    标签: python list pandas file file-copying


    【解决方案1】:

    所以这就是我解决它的方法。 这是一个非常愚蠢的错误:|不加入它的基本路径。 出于性能目的,我将其更改为使用shutil.copyobj,但问题只能通过以下方式解决:

    os.path.join(path,file)
    

    在添加之前,我实际上是从列表中的文件名中读取,而不是加入从实际文件中读取的基本路径:|

    for rownumber in df['row_number'].unique():
        appended = df[df['row_number']==rownumber]['file'].tolist()
        print(appended)
        if (len(appended) == 1):
            shutil.copy(os.path.join(path, appended[0]), new_path)
        else:
            with open(appended[0], "w+") as myappendedfile:
                for file in appended:
                    with open(os.path.join(path,file),'r+') as fd:
                        shutil.copyfileobj(fd, myappendedfile, 1024*1024*10)
                        myappendedfile.write('\n')
            shutil.copy(appended[0],new_path)
    

    【讨论】:

      【解决方案2】:

      你可以这样做,如果文件太大无法加载,你可以按照Python append multiple files in given order to one big file中的说明使用readlines

      import os,shutil
      file_list=['a.txt', 'a1.txt', 'a2.txt', 'a3.txt']
      new_path=
      
      with open(file_list[0], "a") as content_0:
          for file_i in file_list[1:]:
              f_i=open(file_i,'r')
              content_0.write('\n'+f_i.read())
              f_i.close()
      shutil.copy(file_list[0],new_path)
      

      【讨论】:

      • 感谢您的回复。那很奇怪,但它还没有附加。我将使用我的整个代码进行更新,除了我有其他列之外,它是相同的,所以我根据我的条件查询了其中的一部分
      • 您的代码没有附加文件的内容,而是附加文件的名称
      • 这很有趣,我在简单的任务中做到了。不管怎样,我发现你在后一个答案中已经解决了~
      猜你喜欢
      • 2016-05-26
      • 2018-04-06
      • 1970-01-01
      • 2014-08-07
      • 2017-02-25
      • 2013-12-26
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      相关资源
      最近更新 更多