Search code examples
python-3.xpathgetcwd

file object open with relativ (variable) path


i want to open my selenium webdriver for edge with a relative way. actually i does not work out with my code.

path_main = os.getcwd()
edge_path = str('\\') + 'build\msedgedriver.exe'
path_get = path_main + edge_path
s = service.Service(r'path_get')

the path is correct. Actually the lase code with s = service.Service(r'path_get') is not working. Is it possible to read a variable as link with 'r'?

Can someone help me out to get my code more flexible?


Solution

  • You can try to change build\msedgedriver.exe to build\\msedgedriver.exe in order to fix escape sequence

    The r'' is used for regex formatting, so it is irrelevant to this scenario.
    You can provide the string variable as an argument, or use string formatting (f'{variable}string') for these purposes.

    I don't fully understand what you mean by "read a variable as link", but you can provide the variable as an argument to your Service class initialization.

    The final code would be something like

    path_main = os.getcwd()
    edge_path = '\\build\\msedgedriver.exe'
    path_get = path_main + edge_path
    s = service.Service(path_get)
    

    Although it will work only if the path is right.
    For your purpose, I believe, you can use relative paths (like './' or '../') to get rid of os.getcwd(), so it would look something like s = service.Service('./build/msedgedriver.exe')