Find & replace string in multiple csv files under series of subfolders
Question:
Folder structure
|FolderA
|-Foldera1
| A_a1_001.csv
| A_a1_002.csv
|-Folderb1
| A_b1_001.csv
| A_b1_002.csv
CSV format
a_a1_001.csv
XXX,XXX,XXX,XXX
123,456,789,AAA
a_a1_002.csv
XXX,XXX,XXX,XXX
376,452,953,AAA
Request
Find all the csv files in the root to find&replace certain string, and save it back as the same path & filename.
Code
import OS
texttofind='AAA'
texttoreplace='BBB'
sourcepath=os.walk(r"/Users/Gens/Documents/FolderA/")
for file in sourcepath:
inputfile=os.walk(r"/Users/Gens/Documents/FolderA/")
with open (inputfile,'r') as inputfile:
filedata=inputfile.read()
freq=0
freq=filedata.count(texttofind)
destinationpath=os.walk(r"/Users/Gens/Documents/FolderA/")
filedata=filedata.replace(texttofind,texttoreplace)
with open(destinationpath,'w')as file:
file.write(filedata)
file.close()
Questions
It shows:
with open (inputfile,'r') as inputfile:
TypeError: expected str, bytes or os.PathLike object, not generator
Please help me to solve this. Thanks!
Answers:
os.walk()
returns a 3-tuple
So, walk the tree finding files that end with .csv. Open in mode r+, read entire content and just do a string replace. Seek to BOF, write the (possibly) modified text then truncate.
from os import walk
from os.path import join
FIND = 'AAA'
REPLACE = 'BBB'
ROOT = '/Users/Gens/Documents/FolderA/'
for dirpath, _, filenames in walk(ROOT):
for filename in filenames:
if filename.endswith('.csv'):
with open(join(dirpath, filename), 'r+') as csv: # note the open mode
text = csv.read().replace(FIND, REPLACE)
# seek to BOF
csv.seek(0)
csv.write(text)
# truncate is necessary in case the output is smaller than the original input
csv.truncate()
Folder structure
|FolderA
|-Foldera1
| A_a1_001.csv
| A_a1_002.csv
|-Folderb1
| A_b1_001.csv
| A_b1_002.csv
CSV format
a_a1_001.csv
XXX,XXX,XXX,XXX
123,456,789,AAA
a_a1_002.csv
XXX,XXX,XXX,XXX
376,452,953,AAA
Request
Find all the csv files in the root to find&replace certain string, and save it back as the same path & filename.
Code
import OS
texttofind='AAA'
texttoreplace='BBB'
sourcepath=os.walk(r"/Users/Gens/Documents/FolderA/")
for file in sourcepath:
inputfile=os.walk(r"/Users/Gens/Documents/FolderA/")
with open (inputfile,'r') as inputfile:
filedata=inputfile.read()
freq=0
freq=filedata.count(texttofind)
destinationpath=os.walk(r"/Users/Gens/Documents/FolderA/")
filedata=filedata.replace(texttofind,texttoreplace)
with open(destinationpath,'w')as file:
file.write(filedata)
file.close()
Questions
It shows:
with open (inputfile,'r') as inputfile:
TypeError: expected str, bytes or os.PathLike object, not generator
Please help me to solve this. Thanks!
os.walk()
returns a 3-tuple
So, walk the tree finding files that end with .csv. Open in mode r+, read entire content and just do a string replace. Seek to BOF, write the (possibly) modified text then truncate.
from os import walk
from os.path import join
FIND = 'AAA'
REPLACE = 'BBB'
ROOT = '/Users/Gens/Documents/FolderA/'
for dirpath, _, filenames in walk(ROOT):
for filename in filenames:
if filename.endswith('.csv'):
with open(join(dirpath, filename), 'r+') as csv: # note the open mode
text = csv.read().replace(FIND, REPLACE)
# seek to BOF
csv.seek(0)
csv.write(text)
# truncate is necessary in case the output is smaller than the original input
csv.truncate()