Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

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

Options
  • 27-04-2021 7:34pm
    #1
    Registered Users Posts: 5,553 ✭✭✭


    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 Posts: 6,149 ✭✭✭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")
    


  • Registered Users Posts: 3,078 ✭✭✭salonfire


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


  • Registered Users 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