使用Python将多个文件按数字顺序连接或附加到新文件



我将首先说我已经看到了类似问题的答案。我试图用这个来解决我的问题,但我无法让它正常工作。

我有多个.xyz文件,它们具有从0开始的相同前缀和后缀。可能会有100多个文件,因此不单独定义它们是至关重要的。我需要将这些文件按数字顺序合并/连接/附加到merged.xyz。例如:

output0.xyz:

H 1.0 1.0 1.0

输出1.xyz:

H 2.0 2.0 2.0

merged.xyz:

H 1.0 1.0 1.0
H 2.0 2.0 2.0

我试过将其作为一个简单的测试用例:

tempfiles = ['output0.xyz', 'output1.xyz']
f = open("bigtextfile.xyz", "w")
for tempfile in tempfiles:
    f.write(tempfile.read())

但我得到了:

Traceback (most recent call last):
  File "Test.py", line 28, in <module>
    f.write(tempfile.read())
AttributeError: 'str' object has no attribute 'read'

我尝试过的另一种选择:

prefix = "output"
suffix = ".xyz"
count = 0
while count <= 1:
    for filename in os.listdir('./'):
        if filename.endswith(suffix):
            xyzFile = open(prefix + str(count) + suffix, "r")
            lines = xyzFile.readlines()
            xyzFile.close()
            merged = open("merged" + suffix, "a")
            for line in lines:
                merged.write(line)
            merged.close()
    count += 1

但它复制第一个文件两次,第二个复制三次:

H 1.0 1.0 1.0
H 1.0 1.0 1.0
H 2.0 2.0 2.0
H 2.0 2.0 2.0
H 2.0 2.0 2.0

你建议我如何前进?我倾向于第二种解决方案,因为它已经自动按数字顺序获取文件的内容。如果你需要任何额外的细节以及如何改进这个问题,请告诉我。

您有:

tempfiles = ['output0.xyz', 'output1.xyz']
f = open("bigtextfile.xyz", "w")
for tempfile in tempfiles:
    f.write(tempfile.read())

但是循环中的tempfile被分配了一个文件的名称,例如"output0.xyz",并且作为一个字符串,它没有方法read;您需要将其用作open:的参数

tempfiles = ['output0.xyz', 'output1.xyz']
with open("bigtextfile.xyz", "w") as f:
    for tempfile in tempfiles:
        with open(tempfile) as infile:
            f.write(infile.read())

请注意,我使用的是with上下文管理器,当它们所管理的块终止时,它将自动关闭文件。

更新

此代码将尝试处理文件ouput0.xyz、output1.xyz…,并将终止,直到出现打开错误为止。它没有明确读取目录,而是假设文件的编号为0、1、2。。。N:

from itertools import count

with open("bigtextfile.xyz", "w") as f:
    for suffix in count(0): # suffix takes on values 0, 1, 2, ...
        try:
            with open(f'output{suffix}.xyz') as infile:
                f.write(infile.read())
        except Exception:
            break # get out of loop when we can no longer open outputN.xyz for some value N
tempfiles = ['output0.xyz', 'output1.xyz']
with open("bigtextfile.xyz", "w") as f:
    for tempfile in tempfiles:
        with open(tempfile, 'r') as temp:
            f.write(temp.read())
    

最新更新