【问题标题】:how to copy list of files into another file? [duplicate]如何将文件列表复制到另一个文件中? [复制]
【发布时间】:2012-11-07 17:39:39
【问题描述】:

可能重复:
How do I concatenate files in Python?

def copy_file(file_name,copy_file_name,copies):
    i=0
    a=open(file_name,'r')
    f=open(copy_file_name,'w')
    for line in a:
        while i<copies:
            f.write(line)
            i+=1
    a.close()
    f.close()
    return 
copy_file("D:\student\example2.txt","D:\student\copy_file_name.txt",3)

我需要将一个文本文件复制 3 次到另一个文件,并且循环在第一行之后停止:(

def merge_file(list,file_name):
    for i in range(len(list)):
        a=open(list[i],'r')
        f=open(file_name,'w')
        f.write(list[i])
    f.close
    a.close
    return
merge_file([("D:\student\example2.txt"),("D:\student\example3.txt")],"D:\student\copy_file_name.txt")

我需要将文件列表复制到一个文件中。

【问题讨论】:

  • 您可能需要更具体地了解您实际询问的内容。另外,每次调用变量list 时,都会隐藏一个内置函数...

标签: python


【解决方案1】:

您想附加文件,使用 open(filename, 'a'),另请参阅:How do you append to a file?

【讨论】:

    【解决方案2】:

    要将一个文件的内容复制到另一个文件 3 次,您可以执行以下操作:

    with open('outfile.txt','w') as outFile:
        for _ in range(3):
            with open('infile.txt','r') as inFile:
                for line in inFile:
                    outFile.write(line)
    

    要将文件列表的内容复制到另一个文件中,您可以执行以下操作:

    def merge_file(fileList, outFileName):
        with open(outFileName, 'w') as outFile:
            for fileName in fileList:
                with open(fileName, 'r') as inFile:
                    for line in inFile:
                        outFile.write(line)
    

    这两个都未经测试,但它们应该可以工作

    【讨论】:

      【解决方案3】:

      使用shutil.copyfileobj 为您制作副本。请注意,这种方法完全不知道输入文件中的任何编码问题和平台特定的行分隔符。将复制纯字节流。

      import shutil
      
      # the file to copy to
      outfname = "D:\student\copy_file_name.txt"
      
      # the files to copy from
      infnames = ["D:\student\example2.txt", "D:\student\example3.txt"]
      
      # the copy procedure
      with open("outfile", 'w') as outfile:
          for fname in infnames:
              shutil.copyfileobj(open(fname, 'rb'), outfile)
      

      如果您想将单个文件的内容复制给定次数,只需相应地组成infnames

      # the file to copy from n_repetitions times
      infnames = ["D:\student\example2.txt"] * n_repetitions
      
      # same as above
      

      【讨论】:

        【解决方案4】:

        您对 merge_file 的调用正在传递 len 1 的列表,其中单个项目是 2 元组。

        而不是你所拥有的:

        merge_file([("D:\student\example2.txt"),("D:\student\example3.txt")],"D:\student\copy_file_name.txt")
        

        试试这个(我想这就是你的意思:

        merge_file(["D:\student\example2.txt","D:\student\example3.txt"],"D:\student\copy_file_name.txt")
        

        我希望你能看到不同之处。如果您是 python 和列表和元组的新手,我建议您学习一下:http://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange

        【讨论】:

        • 你需要一个额外的, 来获取单个元素元组:("test",)。这只是一个字符串("test")
        【解决方案5】:

        如果我理解正确:

        import fileinput
        with open('output.txt') as fout:
            fout.writelines(fileinput.input(your_list))
        

        即“从your_list中指定的文件名中取出每一行并将它们写入output.txt

        【讨论】:

          猜你喜欢
          • 2014-12-05
          • 2020-12-15
          • 1970-01-01
          • 2022-01-04
          • 1970-01-01
          • 1970-01-01
          • 2012-05-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多