Python Forum
Replace string in many files in a folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace string in many files in a folder
#8
(Oct-14-2019, 10:42 AM)snippsat Wrote: Can use in_place which make it easier the read/write to same place in one go.
import os
import in_place

for files in os.scandir('.'):
    if files.name.endswith('.LIC'):
        with in_place.InPlace(files) as f:
            text = f.read().replace('abcd', 'efgh')
            f.write(text)

Idea Angel
Reply


Messages In This Thread
RE: Replace string in many files in a folder - by Malt - Oct-14-2019, 12:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 643 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Need to replace a string with a file (HTML file) tester_V 1 815 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Rename files in a folder named using windows explorer hitoxman 3 807 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  splitting file into multiple files by searching for string AlphaInc 2 975 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Rename all files in a folder hitoxman 9 1,624 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  Replace string in a nested Dictianory. SpongeB0B 2 1,273 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Replace with upper(string) WJSwan 7 1,666 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  How to loop through all excel files and sheets in folder jadelola 1 4,632 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Printing a raw string with a folder separator at the end, duplicates the separator krulah 5 1,287 Nov-28-2022, 12:41 PM
Last Post: snippsat
  python gzip all files from a folder mg24 3 4,217 Oct-28-2022, 03:59 PM
Last Post: mg24

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020