【问题标题】:Looking for Simple Python Formula to Combine Two Text Files寻找简单的 Python 公式来组合两个文本文件
【发布时间】:2020-02-06 04:28:31
【问题描述】:

这里是 Python 初学者。我正在尝试编写一个将合并两个文本文件的公式。如在,第一个文件应该有第二个简单的添加的文本,而不是替换任何一个文件中的任何内容。

这是我目前所拥有的:

def merge(file1,file2):
    infile = open(file2,'r')
    infile.readline()
    with open('file1','a') as myfile:
        myfile.write('infile')
        myfile.close()

任何帮助将不胜感激。

【问题讨论】:

标签: python file text text-files


【解决方案1】:

您似乎有正确的想法,一种可以简化且更易于阅读的方法是我在 google 上找到的以下代码,适合您的方法。

def merge(file1,file2):
    fin = open(file2, "r")
    data2 = fin.read()
    fin.close()
    fout = open(file1, "a")
    fout.write(data2)
    fout.close()

【讨论】:

  • 似乎是fout = open(file1., "a")中的错字
  • 谢谢。固定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 2014-07-12
  • 1970-01-01
  • 2022-01-07
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多