Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    How extend the “action” range of a script

    General Discussion
    3
    9
    117
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • claudio pergolin
      claudio pergolin last edited by

      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

      PeterJones Alan Kilborn 2 Replies Last reply Reply Quote 0
      • PeterJones
        PeterJones @claudio pergolin last edited by PeterJones

        @claudio-pergolin ,

        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

        1 Reply Last reply Reply Quote 1
        • Alan Kilborn
          Alan Kilborn @claudio pergolin last edited by Alan Kilborn

          @claudio-pergolin

          Typically what you are wanting is done with a Python function called os.walk().
          There’s a nice example in a recent posting HERE.

          1 Reply Last reply Reply Quote 2
          • claudio pergolin
            claudio pergolin last edited by

            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

            Alan Kilborn 1 Reply Last reply Reply Quote 0
            • Alan Kilborn
              Alan Kilborn @claudio pergolin last edited by

              @claudio-pergolin

              What is the console.write( part supposed to be doing?
              I’d just drop that and make your line:

              editor.replace("old", "new")

              1 Reply Last reply Reply Quote 1
              • claudio pergolin
                claudio pergolin last edited by

                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

                Alan Kilborn 1 Reply Last reply Reply Quote 0
                • Alan Kilborn
                  Alan Kilborn @claudio pergolin last edited by

                  @claudio-pergolin

                  Well, with that kind of information provided, there isn’t much someone on this end of things can say.

                  1 Reply Last reply Reply Quote 0
                  • claudio pergolin
                    claudio pergolin last edited by

                    hi Alan,
                    i can’t solve my problem.
                    What info would you need? Thanks

                    Alan Kilborn 1 Reply Last reply Reply Quote 0
                    • Alan Kilborn
                      Alan Kilborn @claudio pergolin last edited by Alan Kilborn

                      @claudio-pergolin

                      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).

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post
                      Copyright © 2014 NodeBB Forums | Contributors