2

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.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257

2 Answers2

4

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.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • Is there any way to get over the problem that arises if my username is two words, i.e there's a break in the statement? for example if the path is - C:/Users/Raghav Sinha/AppData – Raghav Sinha Jun 20 '20 at 13:05
  • Try putting quotes around the path to the executable: `os.system('"C:/Users/Raghav Sinha/AppData/Local/Microsoft/Teams/Update.exe" --processStart "Teams.exe"')` – ChrisGPT was on strike Jun 20 '20 at 13:10
  • still getting it: ```'C:/Users/Raghav' is not recognized as an internal or external command,``` operable program or batch file. – Raghav Sinha Jun 20 '20 at 13:23
  • though I cant test it, I'll assume that it works. let me try and figure out a way around this. Thanks a lot. – Raghav Sinha Jun 20 '20 at 13:26
  • @RaghavSinha, you're using exactly the command I put in my comment? With quotes starting before `C:/` and ending after `Update.exe`? – ChrisGPT was on strike Jun 20 '20 at 13:40
  • yes. maybe it ignores any repeating quotes inside existing single quotes? – Raghav Sinha Jun 20 '20 at 13:49
  • 1
    I don't have a space in my username, but I can successfully run commands with spaces in the path using `os.system()`, e.g. `os.system(r'"C:\Program Files\PuTTY\putty.exe"')`. I'm only using a raw string because of the backslashes—if you're using forward slashes you shouldn't need that. – ChrisGPT was on strike Jun 20 '20 at 13:50
1
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 ✔

GooDeeJAY
  • 1,681
  • 2
  • 20
  • 27