Changing strings in files

Eli the Bearded * at eli.users.panix.com
Tue Nov 10 13:33:54 EST 2020


In comp.lang.python, Loris Bennett <loris.bennett at fu-berlin.de> wrote:
> Manfred Lotz <ml_news at posteo.de> writes:
> > 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.
> 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' {} \;

That 100% fails the "check if a text file" part.

> Having said that, I would be interested to know what the most compact
> way of doing the same thing in Python might be.

Read first N lines of a file. If all parse as valid UTF-8, consider it text.
That's probably the rough method file(1) and Perl's -T use. (In
particular allow no nulls. Maybe allow ISO-8859-1.)

Elijah
------
pretty no nulls is file(1) check


More information about the Python-list mailing list