pythonpython-3.xfile-not-foundpython-os

FileNotFoundError: [WinError 2] The system cannot find the file specified: '%ProgramData%\\Microsoft\\app.exe' in python


I am trying to start a process using os.startfile . But when I run it, I get the following error!

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-4-3c26e36152b3> in <module>
      1 import os
----> 2 os.startfile(r"%ProgramData%\Microsoft\DeviceSync\app.exe")

FileNotFoundError: [WinError 2] The system cannot find the file specified: '%ProgramData%\\Microsoft\\DeviceSync\\app.exe'

But if I use the full path

import os
os.startfile(r"C:\ProgramData\Microsoft\DeviceSync\app.exe")

It runs fine.
What am I doing wrong? :(


Solution

  • For Windows
    So looking at your problem what you can do is actuall create a short of the file you want to start
    For example, create a shortcut like. Right-click your EXE file and from the drop-down menu select the Create Shortcut option.

    App.exe
    App.exe.lnk
    

    Then should Try

    import os      
    x=os.path.expandvars(r"%ProgramData%\Microsoft\DeviceSync\app.exe.lnk")                          
    os.startfile(x)
    

    It should Work Normally and give you the output