我想对每个行执行一项操作,对于以特定目录中的扩展名(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)
答案 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)