Frage

So I have a Python script which I make into an exe with py2exe and I want it to do certain tasks only when the exe version is running. Is there a way to write the code so I don't have to manually save a separate version before I create the exe?

I'm picturing something like this:

if self.filename[-4:] == ".exe":
    do this code

So it would somehow be able to find its own file name. Can it be done?

War es hilfreich?

Lösung

Try

import sys
...
if sys.argv[0].endswith('.exe'):
   ...

Andere Tipps

if ".exe" in self.fileName:
    print "it's an exe file!"

Given that self.filename = "someprogram.exe"

The py2exe docs give a way to do this:

http://www.py2exe.org/index.cgi/HowToDetermineIfRunningFromExe

import imp, os, sys

def main_is_frozen():
    return (hasattr(sys, "frozen") or # new py2exe
            hasattr(sys, "importers") # old py2exe
            or imp.is_frozen("__main__")) # tools/freeze
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top