Oct-21-2016, 04:53 PM
1) it's easier to understand what you want to do if you use English names for your variables
2) it's less painful on the eyes if you use lowercase
3) I don't expect a function call find_word_in_file() to write the file.
4) on the whole, you should never read and write the same file. There is only one read-write pointer, so once you have done the readlines(), that pointer is at the end of the file, and the lines you write are appended at the end of the file. You could use seek statements to force the pointer where you want it, buy this would work only in this very specific case where you replace a string with a string of the same length. The usual way is to open your source in read mode, and open a second file in write mode (use a temporary name created with tempfile.mkstemp), copy the data over (with possible modifications), close that temporary file, then erase the source file and rename the temporary file(*).
(*) Even safer: close temp, rename source to some temp name, rename temp from source, erase source.
2) it's less painful on the eyes if you use lowercase
3) I don't expect a function call find_word_in_file() to write the file.
4) on the whole, you should never read and write the same file. There is only one read-write pointer, so once you have done the readlines(), that pointer is at the end of the file, and the lines you write are appended at the end of the file. You could use seek statements to force the pointer where you want it, buy this would work only in this very specific case where you replace a string with a string of the same length. The usual way is to open your source in read mode, and open a second file in write mode (use a temporary name created with tempfile.mkstemp), copy the data over (with possible modifications), close that temporary file, then erase the source file and rename the temporary file(*).
(*) Even safer: close temp, rename source to some temp name, rename temp from source, erase source.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Your one-stop place for all your GIMP needs: gimp-forum.net