Search code examples
python-3.9

How can I write the code to open an app without entering a specific username


How can I write os.getlogin to open an app like zoom using the code given below

subprocess.Popen('C:\\Users\\srini\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe')

I first tried this subprocess.Popen('C:\\Users\\', os.getlogin, '\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe')

The code is running but the app is not being opened by python I had imported subprocess and os

I tried this code too just to see that if it works os.path.join(os.path.expandvars("%userprofile%"),"AppData", "Roaming", "Zoom", "bin", "Zoom.exe")

But it's still the same

Is there any way to open and app without writing the path or a way that makes this task easier?

                                  👆👆👆👆👆👆👆

I want to write this code for almost all apps on my laptop so are there any codes that can help me in the way mentioned in the above question

BTW I am a beginner and I know few codes, so plz help me understand this.


Solution

  • I think you are on the right track using os.getlogin(). You can also use getlogin.getuser() or os.getenv('username').

    Look at this method to create your path.

    path = os.path.join("c:\\Users",os.getlogin(), "AppData", "Roaming", "Zoom", "bin", "Zoom.exe")
    subprocess.Popen(path)
    

    or in one line with f-strings

    subprocess.Popen(f"c:\\Users\\{os.getlogin()}\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe")