I have a few thousand files that all have specific bits of texts that need to be replaced with the file name of that particular file. After a bit of searching, I think Python is the best was to do this but it isn't something i'm very familiar with so am using the nppp python plugin.
Essentially, I have a directory (Y:\dataFiles) that contains all of the xml files that need editing, i was hoping to run a script to edit .xml in Y:\dataFiles and replace 'replaceData' with .xml
this is what I have so far but get a syntax error on the *
os.chdir('Y:\dataFiles')
for f in *.xml; do
editor.rereplace('replaceData', notepad.getCurrentFilename())
notepad.save()
notepad.close()
done
Thanks in advance,
Ashton
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
First you cahnge to the directory (as you already did)
then you create a list of all files which end with .xml
now iterate over that list and
open it (otherwise you cannot use editor object)
replace the data
save and close it.
Not tested but I assume it should do it. Maybe you wanna test first with a directory
containing only one xml file and see the result.
Cheers
Claudia
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks very much for getting back to me, this worked perfectly except that it kept changing \ for \ in the path name. i got around this by doing os.chdir('Y:')
os.chdir('dataFiles')
Messy, but it worked.
thanks again.
Ashton
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Good eveing,
hoping someone can help.
I have a few thousand files that all have specific bits of texts that need to be replaced with the file name of that particular file. After a bit of searching, I think Python is the best was to do this but it isn't something i'm very familiar with so am using the nppp python plugin.
Essentially, I have a directory (Y:\dataFiles) that contains all of the xml files that need editing, i was hoping to run a script to edit .xml in Y:\dataFiles and replace 'replaceData' with .xml
this is what I have so far but get a syntax error on the *
Thanks in advance,
Ashton
This is not really python syntax ;-)
First you cahnge to the directory (as you already did)
then you create a list of all files which end with .xml
now iterate over that list and
open it (otherwise you cannot use editor object)
replace the data
save and close it.
Not tested but I assume it should do it. Maybe you wanna test first with a directory
containing only one xml file and see the result.
Cheers
Claudia
Hi Claudia,
Thanks very much for getting back to me, this worked perfectly except that it kept changing \ for \ in the path name. i got around this by doing
os.chdir('Y:') os.chdir('dataFiles')
Messy, but it worked.
thanks again.
Ashton
When you use \ inside of a string you need to double it.
Example: os.chdir('Y:\\dataFiles')
Last edit: Sasumner 2017-05-26