Manfred Lotz <ml_n...@posteo.de> writes: > On Tue, 10 Nov 2020 08:19:55 +0100 > "Loris Bennett" <loris.benn...@fu-berlin.de> wrote: > >> Manfred Lotz <ml_n...@posteo.de> writes: >> >> > I have a situation where in a directory tree I want to change a >> > certain string in all files where that string occurs. >> > >> > My idea was to do >> > >> > - os.scandir and for each file >> > - check if a file is a text file >> > - if it is not a text file skip that file >> > - change the string as often as it occurs in that file >> > >> > >> > What is the best way to check if a file is a text file? In a script >> > I could use the `file` command which is not ideal as I have to grep >> > the result. In Perl I could do -T file. >> > >> > How to do best in Python? >> >> If you are on Linux and more interested in the result than the >> programming exercise, I would suggest the following non-Python >> solution: >> >> find . -type -f -exec sed -i 's/foo/bar/g' {} \; >> > > My existing script in Perl which I wanted to migrate to Python I used > `-T $file` and called sed > > I like the -T which I assume does some heuristics to tell me if a file > is a text file.
Sorry, I missed the bit about text files. By '-T' I assume you mean Perl's taint mode option. I am no security expert, but as I understand it, taint mode does more than just check whether something is a text file, although the "more" probably applies mainly to files which contain Perl code. Sorry also to bang on about non-Python solutions but you could do find . -type f -exec grep -Iq . {} \; -and -exec sed -i 's/foo/bar/g' {} \; i.e. let grep ignore binary files and quietly match all non-binary files. -- This signature is currently under construction. -- https://mail.python.org/mailman/listinfo/python-list