Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your case is a particular case: <strong>'newtext'</strong> has exactly the same number of characters as <strong>'oldtext'</strong>. </p> <p>Hence, it is possible to use one of the following codes to replace exactly a word <strong>'oldtext'</strong> or a line in which the word <strong>'oldtext'</strong> is present, by word <strong>'newtext'</strong> or a line in which <strong>'newtext'</strong> replaces <strong>'oldtext'</strong>.</p> <p>.</p> <p>If files have not super-big size, the content of each file can be read entirely into memory:</p> <pre><code>from os import fsync # code using find() count = 0 for match in all_files('*.html', '.'): with open(match,'rb+') as thefile: diag = False fno = thefile.fileno() content = thefile.read() thefile.seek(0,0) x = content.find('oldtext') while x&gt;=0: diag = True thefile.seek(x,1) thefile.write('newtext') thefile.flush() fsync(fno) x = content[thefile.tell():].find('oldtext') if diag: cnt += 1 </code></pre> <p>or</p> <pre><code>from os import fsync # code using a regex import re pat = re.compile('oldtext') count = 0 for match in all_files('*.html', '.'): with open(match,'rb+') as thefile: diag = False fno = thefile.fileno() content = thefile.read() thefile.seek(0,0) prec = 0 for mat in pat.finditer(content): diag = True thefile.seek(mat.start()-prec,1) thefile.write('newtext') thefile.flush() fsync(fno) prec = mat.end() if diag: cnt += 1 </code></pre> <p>.</p> <p>For heavy files, a reading and rewriting line after line is possible:</p> <pre><code>from os import fsync # code for big files, using regex import re pat = re.compile('oldtext') count = 0 for match in all_files('*.html', '.'): with open(match,'rb+') as thefile: diag = False fno = thefile.fileno() line = thefile.readline() while line: if 'oldtext' in line: diag = True thefile.seek(-len(line),1) thefile.write(pat.sub('newtext',line)) thefile.flush() fsync(fno) line = thefile.readline() if diag: cnt += 1 </code></pre> <p>.</p> <p>The instructions <code>thefile.flush()</code> and <code>fsync(fno)</code> are required after each writing in order that the file handler <code>thefile</code> points with accuracy on the exact position in the file at any moment. They allow to obtain effective writing ordered by instuction <code>write()</code> </p> <blockquote> <p>flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior. <a href="http://docs.python.org/library/stdtypes.html#file.flush" rel="nofollow">http://docs.python.org/library/stdtypes.html#file.flush</a></p> </blockquote> <p>.</p> <p>These programs do the minimum. So I think they are fast.</p> <p>.</p> <p>Nota bene <strong>:</strong> a file opened in mode <code>'rb+'</code> have no changing of its time of last modification if no modification has been performed.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload