如何对以特定扩展名结尾的目录中的每个文件中的每一行执行操作?

时间:2016-04-04 06:04:55

标签: python

我想对每个执行一项操作,对于以特定目录中的扩展名(pdb)结尾的所有文件。在我的下面的代码中,我可以理解的是,只有 {lines = pdbinput.read()} 将不允许我在上执行startswith()操作线

import glob, os
os.chdir("F:/python")
for files in glob.glob("*.pdb"):
    with open(files) as pdbinput:
        lines = pdbinput.read()
        with open ('output.txt', 'w') as f1:
            for line in lines:
                if line.startswith("HETATM"):
                    print (line, file=f1)

但它允许我直接打印

import glob, os
os.chdir("F:/python")
for files in glob.glob("*.pdb"):
    with open(files) as pdbinput:
        lines = pdbinput.read()
        with open ('modified.txt', 'w') as f1:
            print (lines, file=f1)

1 个答案:

答案 0 :(得分:0)

pdbinput.read()将整个文件读入一个字符串。这意味着后续的for line in lines将逐个字符地迭代该字符串,而不是逐行。

您可以使用pdbinput.readlines()或(通常的方式)直接迭代文件:

with open(files) as pdbinput:
    with open ('output.txt', 'w') as f1:
        for line in pdbinput:
            if line.startswith("HETATM"):
                print (line, file=f1)

下一个问题:现在,对于每个PDB文件,生成一个新的output.txt,覆盖前一个。{1}}。只需将with块移到迭代之外:

with open ('output.txt', 'w') as f1:
    for files in glob.glob("*.pdb"):
        with open(files) as pdbinput:
            for line in lines:
                if line.startswith("HETATM"):
                    print (line, file=f1)