擅长:python、mysql、java
<p>编辑:我走错了路。:(</p>
<p>好的,我现在编辑了代码,没有错误。现在的问题是,如果我用<code>python readfiles.py /home/</code>运行它,什么都不会发生?在</p>
<pre><code>#!/usr/bin/python
import glob, os
import sys
#Check to see that path was privided
if len(sys.argv) < 2:
print "Please provide a path"
sys.exit()
#Find files in path given
os.chdir(sys.argv[1])
#Chose the ones with txt extension
for file in glob.glob("*.txt"):
try:
#Read and output first two lines of txt file
with open(file) as f:
line1, line2 = next(f), next(f, "")
print line1 + " " + line2
#Catch exception errors
except IOError:
print "Failed to read " + file
</code></pre>