擅长:python、mysql、java
<p>所以我进一步搜索了一下,发现这个非常有用的帖子[<a href="https://askubuntu.com/questions/352198/reading-all-files-from-a-directory][1]">https://askubuntu.com/questions/352198/reading-all-files-from-a-directory][1]</a></p>
<p>试图把它和我现有的代码结合起来。。。它似乎没有打印到输出文件:|</p>
<pre><code>import sys
import glob
import errno
import re
path = '\*.sum'
files = glob.glob(path)
lattEW = open("Lattice_En.txt", "a")
for name in files: # 'file' is a builtin type, 'name' is a less-ambiguous variable name.
try:
with open(name) as f: # No need to specify 'r': this is the default.
sys.stdout.write(f.read())
for line in name: # looks through every line in the file
if re.match("(.*)(L)ATTICE(.*)", line): #searches the lines for LATTICE
print >>lattEW, name, line,
except IOError as exc:
if exc.errno != errno.EISDIR: # Do not fail if a directory is found, just ignore it.
raise # Propagate other kinds of IOError
</code></pre>