Posts: 56
Threads: 23
Joined: Jul 2021
Aug-06-2021, 10:20 PM
(This post was last modified: Aug-08-2021, 06:04 PM by AlphaInc.)
Hello everybody,
In a previous post I got help to find a string in a text-file, replace it with an increasing number and save it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import re
target = "String"
def str_counter(match_object):
str_counter.count + = 1
return str (str_counter.count)
str_counter.count = 0
with open ( 'input.txt' , 'r' ) as file :
filedata = file .read()
filedata = re.sub(re.escape(target), str_counter, filedata)
a_file = open ( "input.txt" , "w" )
text = filedata
print (text, file = a_file)
a_file.close()
|
Now I would like to expand this and do this for every txt-file inside a folder. Is there a way to go through the whole directory, open every file, replace the string with an increasing counter and then save it to the same file? With my posted code I am able to do this for the one specified file but I'd like to do this for every text-file.
Posts: 1,583
Threads: 3
Joined: Mar 2020
Modify your code above to become a function. Have the function take the name of the file and operate on it.
Then use glob.glob or Path.iterdir() (or os.scandir) to get all the files in a directory that you want. Loop over them and pass the filenames or filepaths to your function.
Posts: 56
Threads: 23
Joined: Jul 2021
(Aug-06-2021, 11:06 PM)bowlofred Wrote: Modify your code above to become a function. Have the function take the name of the file and operate on it.
Then use glob.glob or Path.iterdir() (or os.scandir) to get all the files in a directory that you want. Loop over them and pass the filenames or filepaths to your function.
Sorry to be so annoying but how do i do this exactly?
I'm pretty new to python
Posts: 1,028
Threads: 16
Joined: Dec 2016
As bowlofred already wrote, make a function and then call it for every txt file
1 2 3 4 5 6 7 8 9 10 |
import os
folder_path = "/path/to/folder/"
for root, dirs, files in os.walk(folder_path, topdown = False ):
for name in files:
if name.endswith( ".txt" ):
file_name = os.path.join(root, name)
print (file_name)
|
Posts: 56
Threads: 23
Joined: Jul 2021
Aug-08-2021, 04:17 PM
(This post was last modified: Aug-08-2021, 04:17 PM by AlphaInc.)
(Aug-07-2021, 11:45 AM)Axel_Erfurt Wrote: As bowlofred already wrote, make a function and then call it for every txt file
1 2 3 4 5 6 7 8 9 10 |
import os
folder_path = "/path/to/folder/"
for root, dirs, files in os.walk(folder_path, topdown = False ):
for name in files:
if name.endswith( ".txt" ):
file_name = os.path.join(root, name)
print (file_name)
|
I tried to do it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import os
import re
folder_path = "/home/pi/Workspace/Case_02/"
for root, dirs, files in os.walk(folder_path, topdown = False ):
for name in files:
if name.endswith( ".txt" ):
file_name = os.path.join(root, name)
target = "Format"
def str_counter(match_object):
str_counter.count + = 1
return str (str_counter.count)
str_counter.count = 0
with open (file_name, 'r' ) as file :
filedata = file .read()
filedata = re.sub(re.escape(target), str_counter, filedata)
with open (file_name, 'w' ) as file :
file .write(filedata)
|
I set an example with three different files (Out_01.txt, Out_02.txt, Out_03.txt) but the changes to my file were only written to Out_03.txt.
Edit: I just needed to set the right amount of spaces per indentation level.
Posts: 1,028
Threads: 16
Joined: Dec 2016
You did not create a function and your indentations are disastrous, try this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import os
import re
folder_path = "/home/pi/Workspace/Case_02/"
target = "Format"
def str_counter(match_object):
str_counter.count + = 1
return str (str_counter.count)
str_counter.count = 0
def do_it(file_name):
with open (file_name, 'r' ) as file :
filedata = file .read()
print (filedata)
filedata = re.sub(re.escape(target), str_counter, filedata)
print (filedata)
with open (file_name, 'w' ) as file :
file .write(filedata)
file .close()
for name in os.listdir(folder_path):
if name.endswith( ".txt" ):
file_name = os.path.join(folder_path, name)
do_it(file_name)
|
|