How extend the “action” range of a script
-
hi all, I wrote a simple python script that brings some changes (replacements) to a text file.
I would need to extend the “action” range of this script to more text files, contained in a directory and its subfolders. How can I do? thank you -
Write a loop in PythonScript which opens each file in the active editor window, then performs your actions, then saves and maybe closes each file
-
Typically what you are wanting is done with a Python function called
os.walk()
.
There’s a nice example in a recent posting HERE. -
I have little knowledge of python and what I know led me to this:
import os; import sys; for root, dirs, files in os.walk("C:\Users\xxx\Desktop\a"): for file in files: notepad.open(file) console.write(editor.replace("old","new")) notepad.save() notepad.close()
I would like to replace “old” with “new” in all files contained in the directory “a” and in all its subfolders. When I do it in a .py file it gives me an error :
console.write('editor.replace('Remarks','Osservazioni')') ^ SyntaxError: invalid syntax
I think it is a quotation mark problem but I don’t understand how.
Some help
Thank you -
What is the
console.write(
part supposed to be doing?
I’d just drop that and make your line:editor.replace("old", "new")
-
hi alan thank you for your suggestion, but the script doesn’t work yet.
No error message comes out, and apparently it seems to work, but when I go to look at the files in ‘C: \ Users \ xxx \ Desktop \ a’ and its subdirectory, no changes have been made -
Well, with that kind of information provided, there isn’t much someone on this end of things can say.
-
hi Alan,
i can’t solve my problem.
What info would you need? Thanks -
Well, there are lots of approaches to debugging.
If it were me, I might start by commenting out (use#
before the line to eliminate) that closes a file.
That way you can see that the proper files are getting opened (because they will remain open when the script finishes).