以下代码用于使用目录中的文件名(或多个名称)创建新文件(或多个文件)。但是,它还会在标题中创建一个带波形符的文件,并创建word2find的副本。这是一个示例结果
〜$ 17 Q3 Xcel退休退休.txt
我不明白为什么会这样。任何解释将不胜感激。干杯,BobS
import os, os.path,re, pprint,sys, nltk
sourcedir = 'C:/Users/Public/EnvDef/Proj/1 ErnCls/2 IOUErnClsQrtr/2017 Q3/23a Xcel'
os.chdir (sourcedir)
cwd = os.getcwd()
print ('New Current Working Directory %s' % cwd)
for dirPath, subdirNames, fileList in os.walk(cwd):
for filename in fileList:
with open (filename,'r', encoding="ascii", errors ="surrogateescape") as fin:
FileinA=(fin.read())
print (' ')
#create basename by deleting the file name suffix
basename=os.path.splitext(filename)[0]
print (basename)
#tokenize file
FileinB=nltk.sent_tokenize(FileinA)
word2find = 'retire'
result = [sentence for sentence in FileinB if word2find in sentence]
with open (basename+' '+ word2find +'.txt', 'w', encoding="ascii", errors="surrogateescape") as filetowrite:
filetowrite.write(basename+ ' Retire' + "\n" +"\n")
for line in result:
filetowrite.write(line + "\n")