简体   繁体   English

文件未合并:python

[英]Files are not merging : python

I want to merge the contents of two files into one new output file . 我想将两个文件的内容合并到一个新的输出文件中。

I have read other threads about merging file contents and I tried several options but I only get one file in my output. 我已经阅读了有关合并文件内容的其他线程,并尝试了几种方法,但在输出中仅得到一个文件。 Here's one of the codes that I tried and I can't see anything wrong with it. 这是我尝试过的代码之一,我看不出有什么问题。

I only get one file in my output and even if I switch position of file1 and file2 in the list, i still only get only file1 in my output. 我在输出中仅得到一个文件,即使我切换了文件1和文件2在列表中的位置,我在输出中仍仅得到文件1。

Here is my Code : 这是我的代码:

filenames = ['file1','file2']
with open('output.data', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read())

How can i do this ? 我怎样才能做到这一点 ?

My whole code that leads to merging to these two files 我的整个代码导致合并到这两个文件

source1 = open('A','r')
output = open('file1','w')
output.write(',yes.\n'.join(','.join(line) for line in source1.read().split('\n')))

source1 = open('B', 'r')
output = open('file2','w')
output.write(',no.\n'.join(','.join(line) for line in source2.read().split('\n')))

filenames = ['file1','file2']
with open('output.data', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read())

The first file is being closed/flushed when you rebind output to a new file. output重新绑定到新文件时,第一个文件被关闭/清除。 This is the behaviour of CPython, but it's not good to rely on it 这是CPython的行为,但是依靠它是不好的

Use context managers to make sure that the files are flushed (and closed) properly before you try to read from them 在尝试从文件中读取文件之前,请使用上下文管理器确保文件已正确刷新(并关闭)

with open('A','r') as source1, open('file1','w') as output:
    output.write(',yes.\n'.join(','.join(line) for line in source1.read().split('\n')))

with open('B','r') as source2, open('file2','w') as output:
    output.write(',no.\n'.join(','.join(line) for line in source2.read().split('\n')))

filenames = ['file1','file2']
with open('output.data', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            print("Reading from: " + fname)
            data = infile.read()
            print(len(data))
            outfile.write(data)

There is a fair bit of duplication in the first two blocks. 前两个块中有很多重复项。 Maybe you can use a function there. 也许您可以在其中使用一个功能。

After the edit it's clear where your mistake is. 编辑后,很明显您的错误在哪里。 You need to close (or flush) the file after writing, before it can be read by the same code. 您需要在写入后关闭(或刷新)文件,然后才能用相同的代码读取它。

source1 = open('A','r')
output = open('file1','w')
output.write(',yes.\n'.join(','.join(line) for line in source1.read().split('\n')))
output.close()

source2 = open('B', 'r')
output = open('file2','w')
output.write(',no.\n'.join(','.join(line) for line in source2.read().split('\n')))
output.close()

filenames = ['file1','file2']
with open('output.data', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read())

The reason why the first file is available is because you remove the reference to the file descriptor of file1 by reassigning the variable output to hold the file descriptor for file2 , and it will be closed automatically by Python. 第一个文件可用的原因是因为您通过重新分配变量output来保存file2文件描述符,从而删除了对file1的文件描述符的引用,Python会自动关闭该文件描述符。

As @gnibbler suggested, it's best to use with statements to avoid this kind of problem in the future. 正如@gnibbler所建议的,最好with语句一起使用with以避免将来出现这种问题。 You should enclose the source1 , source2 , and output in a with statement, as you did for the last part. 你应该附上source1source2outputwith声明,因为你做的最后一部分。

You can just combine your read and writes into one with statement (if you don't really need the intermediary files); 您可以将您的读写操作与带语句的语句组合在一起(如果您不需要中间文件); this will also solve your closing problem: 这也将解决您的关闭问题:

with open('A') as a, open('B') as b, open('out.txt','w') as out:
   for line in a:
       out.write(',yes.\n'.join(','.join(line)))
   for line in b:
       out.write(',no.\n'.join(','.join(line)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM