Re: Changing strings in files

2020-11-11 Thread Serhiy Storchaka
10.11.20 11:07, Manfred Lotz пише: > Perhaps better. I like to use os.scandir this way > > def scantree(path: str) -> Iterator[os.DirEntry[str]]: > """Recursively yield DirEntry objects (no directories) > for a given directory. > """ > for entry in os.scandir(path): >

Re: Changing strings in files

2020-11-11 Thread Serhiy Storchaka
10.11.20 22:40, Dennis Lee Bieber пише: > Testing for extension in a list of exclusions would be much faster than > scanning the contents of a file, and the few that do get through would have > to be scanned anyway. Then the simplest method should work: read the first 512 bytes and check if

Re: Changing strings in files

2020-11-10 Thread Chris Angelico
On Wed, Nov 11, 2020 at 10:00 AM Cameron Simpson wrote: > > On 11Nov2020 07:30, Chris Angelico wrote: > >If the script's assuming it'll mostly work on small text files, it > >might be very annoying to suddenly read in a 4GB blob of video file > >just to find out that it's not text. > > You can ab

Re: Changing strings in files

2020-11-10 Thread Cameron Simpson
On 11Nov2020 07:30, Chris Angelico wrote: >If the script's assuming it'll mostly work on small text files, it >might be very annoying to suddenly read in a 4GB blob of video file >just to find out that it's not text. You can abort as soon as the decode fails. Which will usually be pretty early f

Re: Changing strings in files

2020-11-10 Thread Cameron Simpson
On 11Nov2020 07:25, Chris Angelico wrote: >If the main job of the program, as in this situation, is to read the >entire file, I would probably have it read in the first 1KB or 16KB or >thereabouts, see if that has any NUL bytes, and if not, proceed to >read in the rest of the file. But depending o

Re: Changing strings in files

2020-11-10 Thread Chris Angelico
On Wed, Nov 11, 2020 at 6:52 AM Barry Scott wrote: > > > > > On 10 Nov 2020, at 19:30, Eli the Bearded <*@eli.users.panix.com> wrote: > > > > In comp.lang.python, Chris Angelico wrote: > >> Eli the Bearded <*@eli.users.panix.com> wrote: > >>> Read first N lines of a file. If all parse as valid UT

Re: Changing strings in files

2020-11-10 Thread Chris Angelico
On Wed, Nov 11, 2020 at 6:36 AM Eli the Bearded <*@eli.users.panix.com> wrote: > > In comp.lang.python, Chris Angelico wrote: > > Eli the Bearded <*@eli.users.panix.com> wrote: > >> Read first N lines of a file. If all parse as valid UTF-8, consider it > >> text. > >> That's probably the rough me

Re: Changing strings in files

2020-11-10 Thread Barry Scott
> On 10 Nov 2020, at 19:30, Eli the Bearded <*@eli.users.panix.com> wrote: > > In comp.lang.python, Chris Angelico wrote: >> Eli the Bearded <*@eli.users.panix.com> wrote: >>> Read first N lines of a file. If all parse as valid UTF-8, consider it text. >>> That's probably the rough method file

Re: Changing strings in files

2020-11-10 Thread Eli the Bearded
In comp.lang.python, Chris Angelico wrote: > Eli the Bearded <*@eli.users.panix.com> wrote: >> 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.) > IS

Re: Changing strings in files

2020-11-10 Thread Manfred Lotz
On Tue, 10 Nov 2020 10:57:05 +0100 "Loris Bennett" wrote: > Manfred Lotz writes: > > > On Tue, 10 Nov 2020 08:19:55 +0100 > > "Loris Bennett" wrote: > > > >> Manfred Lotz writes: > >> > >> > I have a situation where in a directory tree I want to change a > >> > certain string in all file

Re: Changing strings in files

2020-11-10 Thread Manfred Lotz
On Tue, 10 Nov 2020 22:08:54 +1100 Cameron Simpson wrote: > On 10Nov2020 10:07, Manfred Lotz wrote: > >On Tue, 10 Nov 2020 18:37:54 +1100 > >Cameron Simpson wrote: > >> Use os.walk for trees. scandir does a single directory. > > > >Perhaps better. I like to use os.scandir this way > > > >de

Re: Changing strings in files

2020-11-10 Thread Chris Angelico
On Wed, Nov 11, 2020 at 5:36 AM Eli the Bearded <*@eli.users.panix.com> wrote: > 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.) > ISO-8859-1 is basi

Re: Changing strings in files

2020-11-10 Thread Eli the Bearded
In comp.lang.python, Loris Bennett wrote: > Manfred Lotz 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

Re: Changing strings in files

2020-11-10 Thread Grant Edwards
On 2020-11-10, Manfred Lotz wrote: > What is the best way to check if a file is a text file? Step 1: define "text file" -- Grant -- https://mail.python.org/mailman/listinfo/python-list

Re: Changing strings in files

2020-11-10 Thread Cameron Simpson
On 10Nov2020 10:07, Manfred Lotz wrote: >On Tue, 10 Nov 2020 18:37:54 +1100 >Cameron Simpson wrote: >> Use os.walk for trees. scandir does a single directory. > >Perhaps better. I like to use os.scandir this way > >def scantree(path: str) -> Iterator[os.DirEntry[str]]: >"""Recursively yield D

Re: Changing strings in files

2020-11-10 Thread Chris Angelico
On Tue, Nov 10, 2020 at 9:06 PM Manfred Lotz wrote: > The reason I want to check if a file is a text file is that I don't > want to try replacing patterns in binary files (executable binaries, > archives, audio files aso). > I'd recommend two checks, then: 1) Can the file be decoded as UTF-8? 2)

Re: Changing strings in files

2020-11-10 Thread Manfred Lotz
On Tue, 10 Nov 2020 18:52:26 +1100 Mike Dewhirst wrote: > On 10/11/2020 5:24 pm, Manfred Lotz wrote: > > 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 > >

Re: Changing strings in files

2020-11-10 Thread Manfred Lotz
On Tue, 10 Nov 2020 18:37:54 +1100 Cameron Simpson wrote: > On 10Nov2020 07:24, Manfred Lotz wrote: > >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 > > Us

Re: Changing strings in files

2020-11-10 Thread Loris Bennett
Manfred Lotz writes: > On Tue, 10 Nov 2020 08:19:55 +0100 > "Loris Bennett" wrote: > >> Manfred Lotz 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

Re: Changing strings in files

2020-11-10 Thread Manfred Lotz
On Tue, 10 Nov 2020 08:19:55 +0100 "Loris Bennett" wrote: > Manfred Lotz 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 f

Re: Changing strings in files

2020-11-10 Thread Chris Green
Loris Bennett wrote: > Having said that, I would be interested to know what the most compact > way of doing the same thing in Python might be. > Here's my Python replace script:- #!/usr/bin/python3 # # # String replacement utility # import os import re import sys import shutil def replaceStrin

Re: Changing strings in files

2020-11-10 Thread Mike Dewhirst
On 10/11/2020 5:24 pm, Manfred Lotz wrote: 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

Re: Changing strings in files

2020-11-09 Thread Cameron Simpson
On 10Nov2020 07:24, Manfred Lotz wrote: >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 Use os.walk for trees. scandir does a single directory. > - check if a file is

Re: Changing strings in files

2020-11-09 Thread Loris Bennett
Manfred Lotz 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 >- chang

Changing strings in files

2020-11-09 Thread Manfred Lotz
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 t