我有多个目录和每个目录中的许多文件,我想遍历它们中的每一个。我还想只读取5th
每个文件的行,因此忽略前四行。当我运行脚本而不忽略尝试忽略第一4
行时,它运行良好。这是代码:
import os
#find the present working directory
pwd=os.path.dirname(os.path.abspath(__file__))
#find all the folders in the present working directory.
dirs = [f for f in os.listdir('.') if os.path.isdir(f)]
for directory in dirs:
os.chdir(os.path.join(pwd, directory));
chd_dir = os.path.dirname(os.path.abspath(__file__))
files = [ fl for fl in os.listdir('.') if os.path.isfile(fl) ]
print files
for f in files:
f_obj = open(os.path.join(chd_dir, f), 'r')
for i in xrange(0,4): #ignore the first 4 lines
f_obj.next()
s=f_obj.readline()
print s
f_obj.close()
此脚本给出以下错误:
ValueError: Mixing iteration and read methods would lose data
我不明白为什么 python 认为我会丢失一些数据,我也想知道修复它的工作以及为什么修复它。