我上一個問題的工作,並就死在牆上插入字符串中給定一個文件對象
我有一個文本文件(可能很大)集,我需要申請一個序列過濾器和轉換到它並將其導出到其他地方。
所以我大概有
def apply_filter_transformer(basepath = None, newpath = None, fts= None):
#because all the raw studies in basepath should not be modified, so I first cp all to newpath
for i in listdir(basepath):
file(path.join(newpath, i), "wb").writelines(file(path.join(basepath, i)).readlines())
for i in listdir(newpath):
fileobj = open(path.join(newpath, i), "r+")
for fcn in fts:
fileobj = fcn(fileobj)
if fileobj is not None:
fileobj.writelines(fileobj.readlines())
try:
fileobj.close()
except:
print i, "at", fcn
pass
def main():
apply_filter_transformer(path.join(pardir, pardir, "studies"),
path.abspath(path.join(pardir, pardir, "filtered_studies")),
[
#transformer_addMemo,
filter_executable,
transformer_identity,
filter_identity,
])
和apply_filter_transformer FTS是功能列表,接受一個Python文件對象,並返回一個Python文件對象。我遇到的問題是,當我想將字符串插入到文本對象中時,我得到了無意義的錯誤,並且一整個早晨都陷入了僵局。
def transformer_addMemo(fileobj):
STYLUSMEMO =r"""hellow world"""
study = fileobj.read()
location = re.search(r"</BasicOptions>", study)
print fileobj.name
print fileobj.mode
fileobj.seek(0)
fileobj.write(study[:location.end()] + STYLUSMEMO + study[location.end():])
return fileobj
,這給了我
Traceback (most recent call last):
File "E:\mypy\reg_test\src\preprocessor\preprocessor.py", line 292, in <module>
main()
File "E:\mypy\reg_test\src\preprocessor\preprocessor.py", line 288, in main
filter_identity,
File "E:\mypy\reg_test\src\preprocessor\preprocessor.py", line 276, in apply_filter_transformer
fileobj.writelines(fileobj.readlines())
IOError: [Errno 0] Error
如果誰能給我上的錯誤的詳細信息,我將不勝感激非常非常多。
請修復您的格式。 – 2009-10-29 17:53:02