I'm trying to install ansicon on Windows 8.1. I extracted the files and got to the level that I need to call ansicon -i. When I type this in my cmd and run python scripts that works great but when I call t from python by os.system('ansicon -i') that doesn't work and seems like it doesn't have any influence on the cmd.
Why os.system('ansicon -i') doesn't work and what alternative method can I use from within python?
First off, it’s not the
-iflag that really does the work.-ionly tells it to add itself to AutoRun. The-pflag that-iimplies is what really does the work:-ptells it to inject a DLL into the parent process, and therein lies the problem: when you useos.system, you spawn a shell, which then runs the command you give it. But then you have Python runningcmdrunningansicon, andansiconwill inject intocmd, and thencmd, having finished its work, will exit.Rather than using
os.system, use thesubprocessmodule, e.g.:The
subprocessmodule (unlikeos.system) will execute the command directly without a shell in-between (unless you passshell=True). Then Python will spawnansicon, andansiconwill inject into Python, as desired.That said, rather than having
ansiconinject itself into Python, Python could probably just load the DLL itself, avoiding some hardship: