Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PE_CBL
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stefano Serafin
PE_CBL
Commits
a96e498f
Commit
a96e498f
authored
3 months ago
by
Stefano Serafin
Browse files
Options
Downloads
Patches
Plain Diff
utility script for bulk replacement of text bits in several files
parent
f4dc69f0
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
replace_text.py
+46
-0
46 additions, 0 deletions
replace_text.py
with
46 additions
and
0 deletions
replace_text.py
0 → 100644
+
46
−
0
View file @
a96e498f
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Useful in order to edit
# a set of config files all at once
# https://stackoverflow.com/questions/74191831/how-to-replace-a-text-with-another-text-in-several-txt-files-at-the-same-time
import
glob
import
os
# Input and output text files
pathSource
=
"
./config_files/
"
pathDest
=
"
./config_files_new/
"
textFiles_Source
=
glob
.
glob
(
pathSource
+
"
*.json
"
)
textFiles_Dest
=
[
i
.
replace
(
pathSource
,
pathDest
)
for
i
in
textFiles_Source
]
if
not
os
.
path
.
exists
(
pathDest
):
os
.
makedirs
(
pathDest
)
# searched text and replacement text
search_text
=
'"
nens
"
: 20
'
replace_text
=
'"
nens
"
: 200
'
# Loop over each text file in our list from above
for
source
,
dest
in
zip
(
textFiles_Source
,
textFiles_Dest
):
# Opening our text file in read only
# mode using the open() function
with
open
(
source
,
'
r
'
)
as
file
:
# Reading the content of the file
# using the read() function and storing
# them in a new variable
data
=
file
.
read
()
# Searching and replacing the text
# using the replace() function
data
=
data
.
replace
(
search_text
,
replace_text
)
# Opening our text file in write only
# mode to write the replaced content
with
open
(
dest
,
'
w
'
)
as
file
:
# Writing the replaced data in our
# text file
file
.
write
(
data
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment