Raghav Sinha
Raghav Sinha

Reputation: 35

Can't open Microsoft Teams with python (3.8) script using any method

I am trying to make a script to automate the login into Microsoft Teams and all of my code works except the part where the application has to be opened. The weird thing is that this is capable of opening any other application except MS Teams (Chrome, Notepad, Firefox, Edge etc.)

Here's the relevant code:

def openfile():
    if os.stat("stor.txt").st_size == 0:
        name = filedialog.askopenfilename()
        newfile = open("stor.txt", "w")
        newfile.write(name)

    else:
        name = (open("stor.txt", "r").read())
        os.startfile(name)
        sleep(5)
        keyboard.write(open("user.txt", "r").read())
        keyboard.press("enter")
        sleep(3)
        keyboard.write(open("pass.txt", "r").read())
        keyboard.press("enter")

I tried this with os.startfile, os.system(start..) and every other method on the web. Doesn't work.

The value I'm passing in to os.startfile() when I try to run Teams is C:/Users/Raghav/AppData/Local/Microsoft/Teams/Update.exe.

Upvotes: 2

Views: 2463

Answers (2)

Vijit Kumawat
Vijit Kumawat

Reputation: 26

import os

os.system("C:\\Users\\Lenovo\\AppData\\Local\\Discord\\Update.exe --processStart Discord.exe")

For applications that have an address like above, there are also some tips:

  1. Sometimes Discord.exe name of the file in the address have "Discord.exe" (with double-quotes). Remove it.
  2. Instead of single \ use double \\ in the address.

It will definitely work GO AHEAD ✔

Upvotes: 1

Chris
Chris

Reputation: 136929

First of all, I don't recommend storing your password in plain text like that. It's not very secure, and if another program takes focus at the right time your code will even type your password somewhere else!

Teams should remember your credentials after the first time you log in. I suggest letting it handle that part.

In any case, running os.startfile("foo.exe") is like double-clicking on foo.exe. The file name that you're passing in is C:/Users/Raghav/AppData/Local/Microsoft/Teams/Update.exe, and Update.exe doesn't look like something that should launch Teams to me.

Inspecting the Teams shortcut in my own Start menu, I see that things are a bit more complicated. This shortcut runs Update.exe and passes it some arguments:

C:\...\Update.exe --processStart "Teams.exe"

There is no way to pass arguments to a program with os.startfile(). Try os.system() instead:

os.system('C:/Users/Raghav/AppData/Local/Microsoft/Teams/Update.exe --processStart "Teams.exe"')

There are lots of other ways to run external commands in Python, but this is likely simplest since you don't need Teams' output streams. This command should return 0 if it succeeds and some other value if it fails.

Upvotes: 4

Related Questions