Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Python: Using a regex to change a path allowing for windows and linux

  • 27-04-2021 07:34PM
    #1
    Registered Users, Registered Users 2 Posts: 5,755 ✭✭✭


    So we have a path where we want to change directory relative to where we are.

    The absolute_path variable will have windows or linux style based on the OS used so we had to rejig the code, swapping \ for / to allow for this at the replace part if windows is detected.
            absolute_path = path.dirname(path.abspath(__file__))
            this_is_windows = False
            if '\\' in absolute_path:
                this_is_windows = True # Windows path
                key_path_before = absolute_path.replace('\\', '/')
            else:
                key_path_before = absolute_path
    
            key_path = key_path_before.replace("lib/proj/projx/utils/CE/ce_common_utils",
                                                                        "tests/projx/CE/oci_keys")
    

    Id rather do this in a regex but suck at them

    So i want to replace..

    Everything starting with "lib" and ending with "ce_common_utils" (storing the seperator)

    with

    tests/projx/CE/oci_keys (using the seperator where those "/" are)


Comments

  • Registered Users, Registered Users 2 Posts: 7,150 ✭✭✭Talisman


    You're over thinking the problem, there's no need for regex just use the standard library functions for your purpose.
    file_path = "lib/proj/projx/CE/ce_common_utils"
    
    file_path = os.path.relpath(file_path, "")
    

    On Windows this will transform the string to "lib\\proj\\projx\\CE\\ce_common_utils".

    A function to do the folder path change.
    def change_folder_path(full_path, orig_sub, new_sub):
         full_path = os.path.relpath(full_path, "")
         orig_sub = os.path.relpath(orig_sub, "")
         new_sub = os.path.relpath(new_sub, "")
         return full_path.replace(orig_sub, new_sub)
    

    So in your code you call:
    key_path = change_folder_path(key_path_before,
                                  "lib/proj/projx/utils/CE/ce_common_utils",
                                  "tests/projx/CE/oci_keys")
    


  • Posts: 3,126 ✭✭✭ [Deleted User]


    Google regex tester or regex generator to help you figure what the regex for your string should be


  • Registered Users, Registered Users 2 Posts: 1 Jadgpanzer


    On a side note, whenever you have path parts as strings with no delimiters then you can use:
    os.path.join(part1, part2, part3)
    

    which will automatically join strings using the correct delimiters. As veryangryman said, standard library functions are the way to go


Advertisement