views:

184

answers:

5

I do not know windows well, so that may explain my dilemma ...

I am trying to run bcdedit in Windows 2008R2 from Python 2.6.

My Python routine to run a command looks like this:

def run_program(cmd_str):
    """Run the specified command, returning its output as an array of lines"""
    dprint("run_program(%s): entering" % cmd_str)
    cmd_args = cmd_str.split()
    subproc = subprocess.Popen(cmd_args, stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE, shell=True)
    (outf, errf) = (subproc.stdout, subproc.stderr)
    olines = outf.readlines()
    elines = errf.readlines()
    if Options.debug:
        if elines:
            dprint('Error output:')
            for line in elines:
                dprint(line.rstrip())
        if olines:
            dprint('Normal output:')
            for line in olines:
                dprint(line.rstrip())
    errf.close()
    outf.close()
    res = subproc.wait()
    dprint('wait result=', res)
    return (res, olines)

I call this function thusly:

(res, o) = run_program('bcdedit /set {current} MSI forcedisable')

This command works when I type it from a cmd window, and it works when I put it in a batch file and run it from a command window (as Administrator, of course).

But when I run it from Python (as Administrator), Python claims it can't find the command, returning:

bcdedit is not recognized as an internal or external command,
operable program or batch file

Also, if I trying running my batch file from Python (which works from the command line), it also fails. I've also tried it with the full path to bcdedit, with the same results.

What is it about calling bcdedit from Python that makes it not found?

Note that I can call other EXE files from Python, so I have some level of confidence that my Python code is sane ... but who knows.

Any help would be most appreciated.

A: 

Perhaps the path to bcdedit.exe isn't in your system path when Python is running for some reason (a different user account, for example). You can find this out by printing:

os.environ.get("PATH")

It's semicolon-delimited, so os.environ.get("PATH").split(';') might be more useful.

I can't see any reason why it wouldn't be there, but just in case, you should be looking for C:\Windows\System32, where C is your Windows drive letter.

Samir Talwar
I already tried that, to no avail: same results!
Lee-Man
Goddamnit. Perhaps check the permissions of `bcdedit.exe` to make sure all users can see it?
Samir Talwar
A: 

Check your PATH variable and see if C:\windows\system32 is there. (use set in DOS)

ghostdog74
As mentioned above, I also tried using the full path name, and I can find other (non-hidden) programs in C:\Windows\System32 ...
Lee-Man
A: 

For some reason I experiment the same trouble from c#. If I list the files it was not here, but when I was looking from Explorer it was there. maybe it is some kind of protected file. To call bcdedit.exe, I manually copied it from system32 to my application folder and it worked. There is also another one in windows\winsxs folder. I can start it from my application, but I`m not sure it is the same path on all computers. Hope it help

A: 

Windows 2008 R2 is 64-bit-only, yes? Python's a 32-bit process. When a 32-bit app runs something from C:\Windows\System32, Windows actually looks in C:\Windows\SysWOW64. Use C:\Windows\SysNative.

Roger Lipscombe
That sounds plausible. I'll try that when I get a chance. Thanks!
Lee-Man
A: 

That`s right I tried it and now I can list and use the file. Thank you for the hint