How can I do something like
if sys.argv[0] != '%windir%\\blabla.exe'
I'm having no success at all
How can I do something like
if sys.argv[0] != '%windir%\\blabla.exe'
I'm having no success at all
Use os.environ
to get the value of the environment variable, and string interpolation to put it with the rest of the string:
execfile = r'%s\blabla.exe' % (os.environ['windir'],)
if sys.argv[0] != execfile:
....
You need:
if sys.argv[0] != os.environ.get("windir")
That should do it for you. Just add your blabla.exe
on the end of that.
This should illustrate how to get the location of Windows:
>>> import os
>>> os.environ['windir']
'C:\\Windows'
import os
if sys.argv[0] != os.path.join(os.environ['WINDIR'],'blalah.exe'):
if sys.argv[0] != '%windir%\\blabla.exe'
What are you trying to do here?
argv[0]
is, generally, the filename of the script you are invoking. It would seem unlikely to be blabla.exe
, unless you're using some packaging tool to compile yourself into an EXE. If you want to look at the first argument passed to the script, see sys.argv[1]
.
Also, when you use an %EnvironmentVariable%
from the console, the Windows shell will replace the variable name with its content. So when you type myscript.py %windir%\blabla.exe
, Windows will replace that with myscript.py C:\Windows\blabla.exe
(or similar) before your script gets a look in, and your comparison will fail because the variable name is no longer there.
You can use os.path.expandvars
to do the same replacement to your own strings (or just read os.environ
yourself). You may also want to absolutize the path so you can compare the real effective path without having to worry about relative paths:
blapath= os.path.expandvars(r'%WinDir%\blabla.exe')
if os.path.abspath(sys.argv[1])==os.path.abspath(blapath):
...
In this case, you could also consider os.path.normcase
ing both paths, to allow for Windows's case-insensitivity.