• Login
Community
  • Login

How extend the “action” range of a script

Scheduled Pinned Locked Moved General Discussion
9 Posts 3 Posters 460 Views
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.
  • C
    claudio pergolin
    last edited by Jun 13, 2021, 2:07 PM

    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

    P A 2 Replies Last reply Jun 13, 2021, 2:44 PM Reply Quote 0
    • P
      PeterJones @claudio pergolin
      last edited by PeterJones Jun 13, 2021, 2:45 PM Jun 13, 2021, 2:44 PM

      @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
      • A
        Alan Kilborn @claudio pergolin
        last edited by Alan Kilborn Jun 13, 2021, 11:37 PM Jun 13, 2021, 11:37 PM

        @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
        • C
          claudio pergolin
          last edited by Jul 4, 2021, 8:59 AM

          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

          A 1 Reply Last reply Jul 4, 2021, 11:17 AM Reply Quote 0
          • A
            Alan Kilborn @claudio pergolin
            last edited by Jul 4, 2021, 11:17 AM

            @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
            • C
              claudio pergolin
              last edited by Jul 4, 2021, 6:59 PM

              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

              A 1 Reply Last reply Jul 4, 2021, 7:44 PM Reply Quote 0
              • A
                Alan Kilborn @claudio pergolin
                last edited by Jul 4, 2021, 7:44 PM

                @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
                • C
                  claudio pergolin
                  last edited by Jul 11, 2021, 5:26 PM

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

                  A 1 Reply Last reply Jul 15, 2021, 7:13 PM Reply Quote 0
                  • A
                    Alan Kilborn @claudio pergolin
                    last edited by Alan Kilborn Jul 15, 2021, 7:14 PM Jul 15, 2021, 7:13 PM

                    @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
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors