I wrote a program which read a file (call a) line by line and use this content to search some files (call b) inside a folder, my first version is 2 loop, first loop is openning b and open a each time to search. The second version is open b and save all content to List, and then 2 loop for openning b and the list, I suppose the list will be faster because no file open/read opeartion each time.
However, the speed is slower than the first version very much. Is file open / read is faster than access the list or my program is not good in list using?
Thanks
Chris
Version 1, open a each time to search b
#coding: UTF-8importcodecsimportosimportsysnotepad.runPluginCommand('Python Script','Show Console')filePathSrc=u'C:\\Temp\\a'# Subsitution FilesubsitutionFile=u'C:\\TEMP\\subsitutions.txt'# Program Startconsole.write('Program Start !!\n')console.write(u'Source: '+filePathSrc+'\n')forroot,dirs,filesinos.walk(filePathSrc):console.write('Searching '+root+'\n')forfninfiles:iffn[-4:]==u'.srt'orfn[-4:]==u'.ssa'orfn[-4:]==u'.ass':fileName=root+'\\'+fnconsole.write(u'fileName: '+fileName+'\n')notepad.open(fileName.encode('utf-8'))# replace value in subsitution file, separate values with space# Foramat# A Bwithopen(subsitutionFile)asf:replaceCount=0forlinf:#console.write (l + '\n')s=l.split()iflen(s)>1:startPos=0#Display the result first, replace if foundwhileTrue:pos=editor.findText(FINDOPTION.REGEXP,startPos,editor.getLength(),s[0])ifposisNone:breakelse:ifstartPos==0:console.write('from:'+s[0]+'\t to:'+s[1]+'\n')editor.gotoPos(pos[0])console.write(str(editor.lineFromPosition(editor.getCurrentPos()))+': '+editor.getCurLine())startPos=pos[0]+1ifstartPos!=0:editor.replace(s[0],s[1])replaceCount+=1else:iflen(s)==1:console.writeError('Only 1 element:'+l)f.close()ifreplaceCount>0:notepad.save()notepad.close()
Version 2, save the a into list and use for b
#coding: UTF-8importcodecsimportosimportsysnotepad.runPluginCommand('Python Script','Show Console')#Function definitiondefSubList(subsitutionFile):withopen(subsitutionFile)asf:slist=[]forlinf:#console.write (l + '\n')s=l.split()iflen(s)>1:sList.append(s)else:iflen(s)==1:console.writeError('Only 1 element: %s'%l)f.close()returnsList#End SubListfilePathSrc=u'C:\\Temp\\a'# Subsitution FilesubsitutionFile=u'C:\\TEMP\\subsitutions.txt'# Program Startconsole.write('Program Start !!\n')console.write('Load Subtitle Replacement List from %s'%subsitutionFile)sl=SubList(subsitutionFile)console.write(' Completed\n')console.write(u'Source: '+filePathSrc+'\n')forroot,dirs,filesinos.walk(filePathSrc):console.write('Searching '+root+'\n')forfninfiles:iffn[-4:]==u'.srt'orfn[-4:]==u'.ssa'orfn[-4:]==u'.ass':fileName=root+'\\'+fnnotepad.open(fileName.encode('utf-8'))# replace value in subsitution file, separate values with space# Foramat# A BreplaceCount=0forsinsl:startPos=0# Find and Display resultwhileTrue:pos=editor.findText(FINDOPTION.REGEXP,startPos,editor.getLength(),s[0])ifposisNone:breakelse:ifstartPos==0:console.write('from:'+s[0]+'\t to:'+s[1]+'\n')editor.gotoPos(pos[0])console.write(str(editor.lineFromPosition(editor.getCurrentPos()))+': '+editor.getCurLine())startPos=pos[0]+1# Replace result if foundifstartPos!=0:editor.replace(s[0],s[1])replaceCount+=1ifreplaceCount>0:notepad.save()notepad.close()
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
To be honest, I don't think that reading a file once and using its content in a list is slower
than opening the substitution file always, if you tested this with more than one file.
But a general question, all that you want to do is to find some text and replace it, correct?
If so, why not using
...
notepad.open(fileName.encode('utf-8'))
for s in sl
editor.replace(s[0],s[1])
notepad.save()
notepad.close()
There is no need for doing findText, saving position ... first.
Another hint, you can define the list outside of the function and
append inside of the function, so there is no need to return the list.
Lists aren't affected by the global/local variable issue.
Cheers
Claudia
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I wrote a program which read a file (call a) line by line and use this content to search some files (call b) inside a folder, my first version is 2 loop, first loop is openning b and open a each time to search. The second version is open b and save all content to List, and then 2 loop for openning b and the list, I suppose the list will be faster because no file open/read opeartion each time.
However, the speed is slower than the first version very much. Is file open / read is faster than access the list or my program is not good in list using?
Thanks
Chris
Version 1, open a each time to search b
Version 2, save the a into list and use for b
To be honest, I don't think that reading a file once and using its content in a list is slower
than opening the substitution file always, if you tested this with more than one file.
But a general question, all that you want to do is to find some text and replace it, correct?
If so, why not using
There is no need for doing findText, saving position ... first.
Another hint, you can define the list outside of the function and
append inside of the function, so there is no need to return the list.
Lists aren't affected by the global/local variable issue.
Cheers
Claudia